help ! stuck with maths problem i think..[SOLVED]

hey everyone, i am stuck and rapidly running out of ideas with my maths problem, any pointers would be great…

consider the following… a screen is filled with random pixels the following code only half works and i have tried everything i can think of to get it working but i am at a loss. the pixels on the left hand side move outwards at an angle from the center of the screen,perfect just what i am looking for…
but the pixels on the right hand side when i alter the code do the following go in to the center…dont move at all(code on show) or dont even show ???


						int startX = i;
						int startY = a;
						int dx = 0;
						int dy = 0;
						int endX=0;
						int endY=0;
						
						if(startX <=400){
						dx = 400 - startX;dy = 250 - startY;
						
						
						double distToTarget = Math.sqrt(dx * dx + dy * dy);
						double ratio = 5/ distToTarget ;

						
						endX = startX - (int)Math.round(ratio * dx);
						
						endY = startY - (int)Math.round(ratio * dy);
						}			
						if(startX >=400){
						dx = 400 - startX;dy = 250 - startY;
						
						
						double distToTarget2 = ((dx * dx + dy * dy)*2);
						double ratio2 = 5/ distToTarget2 ;

						
						endX = startX + (int)Math.round(ratio2 * dx);
						
						endY = startY + (int)Math.round(ratio2 * dy);
						}

what am i missing ?

I assume 400,250 is the center of the screen.
Why do you treat points right of the center differently?

I think you should treat all points equally.
Just do this


                  int startX = i;
                  int startY = a;
                  int dx = 0;
                  int dy = 0;
                  int endX=0;
                  int endY=0;
                  
                  dx = 400 - startX;dy = 250 - startY;
                  
                  
                  double distToTarget = Math.sqrt(dx * dx + dy * dy);
                  double ratio = 5/ distToTarget ;

                  
                  endX = startX - (int)Math.round(ratio * dx);
                  
                  endY = startY - (int)Math.round(ratio * dy);
                  

Thanks, but that still just produces half an image on the left hand side going out, theres no right hand side pixels at all

update… if you change the sign at the bottom 2 statements to positive the right hand side pixels cave in to the centre of the screen and the pixels on the right hand side dont show at all :persecutioncomplex:

Try this program

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Pixels extends JPanel {
    static Random rand = new Random();
    Pixel[] pixels;
    final static int width = 800, height = 500;
    
    static class Pixel {
        double x,y;
        Color color;
        
        Pixel(double x, double y, Color color) {
            this.x = x;
            this.y = y;
            this.color = color;
        }
        
        void paint(Graphics g) {
            g.setColor(color);
            g.fillRect((int)x,(int)y,2,2);
        }
    }
    
    public Pixels() {
        pixels = new Pixel[1000];
        for(int i = 0; i < pixels.length; i++) pixels[i] = new Pixel(rand.nextInt(width),
                                                                     rand.nextInt(height),
                                                                     new Color(rand.nextInt(0xFFFFFF)));
    }
    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame();
        Pixels panel = new Pixels();
        frame.add(panel);
        panel.setPreferredSize(new Dimension(width, height));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        for(int i = 0; i<1000;i++) {
            for(Pixel p: panel.pixels) {
                double startX = p.x;
                double startY = p.y;
                double dx = width/2-startX;
                double dy = height/2-startY;
                double distToTarget = Math.sqrt(dx*dx+dy*dy);
                double ratio = 0.5 / distToTarget;
                double endX = startX - ratio*dx;
                double endY = startY - ratio*dy;
                p.x = endX;
                p.y = endY;
            }
            Thread.sleep(10);
            frame.repaint();
        }
    }
    
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,width,height);
        for(Pixel p: pixels) p.paint(g);
    }

YES exactly like that , now to sift through your program to pick out the maths bit, thank you !!

update…

typical…it wasnt the maths after all, i was that thing when you get variable interference from somewhere else in the program, i was zeroing out a 2d array just before my loop started i put it after the array had found a pixels and hey presto it worked, btw thats some weird for loop you use, that form scares me because i dont understand it…haha


	public void imageupdate(){
	
			bimage.createGraphics();	
			int rgb;
		    
			for (int i=0; i<800; i++) {
				for(int a=0;a<500;a++){
						//test2[i][a]=0;    /// /<-----this was the culprit
					if(starsimage[i][a]==1){
						test2[i][a]=0;
						int startX = i;   //// <-no wonder it didnt work
						int startY = a;
								
											
							int dx = 400 - startX;
							int dy = 250 - startY;

See folks thats what happens if a newbie takes 2 years out of programming

foreach

The important part is this:

Applying this to the example:

for(Pixel p: panel.pixels)

…should be read as…

[quote]for [color=red]each(Pixel p in panel.pixels)[/color]
[/quote]
I personally hated it at first, but now I’ve come to like it.
My only gripe with it is the occasional problem it causes when bodging new functionality into old code.
You’ll sometimes find you need access to the Iterator or the iteration index and have to re-factor the loop construct - which can be a bit of a faff.

I sort of understand it, if an iterator is not required, use that form because its faster, but that would mean changing pages of code :o, may be i will experiment next year with it , as im too wrapped up in the understandings of adding user defined classes into linkedlists and manipulating the contents…so much to learn !!