This discussion is archived
5 Replies Latest reply: Jun 1, 2011 12:43 AM by 865172 RSS

Working out coordinates between to given points

805044 Newbie
Currently Being Moderated
Hi guys, simple question here. Lets say I have two points:
point 1

x = 0
y = 0


point 2

x = 10
y = 10
How would i find out all the coordinates inbetween that programmatically, assuming there is a strait line between two points... so the above example would return:
0,0
1,1
2,2
3,3
...
8,8
9,9
10,10
Thanks :)
  • 1. Re: Working out coordinates between to given points
    EJP Guru
    Currently Being Moderated
    I don't understand the question. You have the co-ordinates of both points, which define both ends of the line between them. What is it you don't have?
  • 2. Re: Working out coordinates between to given points
    805044 Newbie
    Currently Being Moderated
    Hey EJP, i am trying to find the coordinates between the two given points .

    lets says i have Point1 as 1,1 and point2 as 5,5

    i'm trying to write the code to find the coordinates between them.

    It should be

    2,2
    3,3
    4,4

    i have come up with a solution but its not 100% correct
    try running the code below
    public class Test extends JPanel implements Runnable {
    
        Point gun = new Point(10, 400);
        Point enemy = new Point(460, 100);
        Point bullet = new Point(0, 0);
        ArrayList<Point> bulletPath = new ArrayList<Point>();
     
        //Constructor
        public Test() {
            Thread thread = new Thread(this);
            thread.start();//Animator thread
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            //Drawing gun
            g.drawRect(gun.x, gun.y, 20, 20);
            g.drawString("Gun", gun.x,gun.y);
            
            //Drawing  Enemy
            g.drawRect(enemy.x, enemy.y, 20, 20);
            g.drawString("Enemy", enemy.x,enemy.y);
            
            //Bullet
            g.fillRect(bullet.x, bullet.y, 5, 5);
        }
    
        public int pointStatus(int gunLocation, int targetLocation) {
            int incr = 1;
            int decrs = 2;
            int equal = 3;
    
            int returnStatus = 0;
            if (gunLocation < targetLocation) {
                returnStatus = incr;
            }
            if (gunLocation > targetLocation) {
                returnStatus = decrs;
            }
    
            if (gunLocation == targetLocation) {
                returnStatus = equal;
            }
            return returnStatus;
        }
    
        public ArrayList<Point> getShootingPath(Point startingLocation, Point targetLocation) {
    
    
            ArrayList<Point> points = new ArrayList<Point>();
            int startXGL = startingLocation.x;
            int startYGL = startingLocation.y;
    
            int xStatus = pointStatus(startingLocation.x, targetLocation.x);
            int yStatus = pointStatus(startingLocation.y, targetLocation.y);
            int currentGunlocx = startingLocation.x;
            int currentgunLocY = startingLocation.y;
            int num = 0;
            while (num < 500) {
    
                startXGL = actionPoint(xStatus, currentGunlocx);
                startYGL = actionPoint(yStatus, currentgunLocY);
                currentGunlocx = startXGL;
                currentgunLocY = startYGL;
                xStatus = pointStatus(startXGL, targetLocation.x);
                yStatus = pointStatus(startYGL, targetLocation.y);
    
                points.add(new Point(startXGL, startYGL));
                num++;
            }
    
    
    
            return points;
        }
    
        public int actionPoint(int pointStatus, int pointNum) {
            int i = pointNum;
            if (pointStatus == 1) {
    
                i++;
            } else if (pointStatus == 2) {
                i--;
            }
    
            return i;
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            Test test = new Test();
            
            frame.add(test);
            frame.setSize(500, 500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);          
        }
  • 3. Re: Working out coordinates between to given points
    gimbal2 Guru
    Currently Being Moderated
    802041 wrote:
    Hey EJP, i am trying to find the coordinates between the two given points .

    lets says i have Point1 as 1,1 and point2 as 5,5

    i'm trying to write the code to find the coordinates between them.
    find any line drawing algorithm on the net. Don't try to roll your own, there are plenty of algorithms that work perfectly. These algorithms will provide you exactly what you want; all the places where a pixel should be drawn to form the line, which are exactly the coordinates you are looking for.
  • 4. Re: Working out coordinates between to given points
    865172 Newbie
    Currently Being Moderated
    Coordinates from A to B
    public static void main(String args[]) throws Exception
        {
            Point a = new Point(0,0);
            Point b = new Point(5,9);
            double distX = b.getX() - a.getX();
            double distY = b.getY() - a.getY();
            double distance = Math.sqrt(distX*distX + distY*distY);
            double angle = Math.atan2(distY, distX);
            // coordinates 
            for (int t=0; t<=distance; t++)
            {
                System.out.println("step "+t+" -> ("+a.x+t*Math.cos(angle)+","+a.y+t*Math.sin(angle)+")");
            }
            System.out.println("step last -> ("+a.x+distance*Math.cos(angle)+","+a.y+distance*Math.sin(angle)+")");
        }
    hope it helps in something
  • 5. Re: Working out coordinates between to given points
    865172 Newbie
    Currently Being Moderated
    A better approach (was too late for me when I wrote last :)):
    public static void main(String args[]) throws Exception
        {
            Point a = new Point(0,0);
            Point b = new Point(5,5);
            double distX = b.getX() - a.getX();
            double distY = b.getY() - a.getY();
            double angle = Math.atan2(distY, distX);
    
            double distance = Math.sqrt(distX*distX + distY*distY);
            int steps = 5;
            double velX = distance/steps * Math.cos(angle);
            double velY = distance/steps * Math.sin(angle);
    
            // coordinates
            System.out.println("Point a -> ("+a.x+","+a.y+")");
            if (steps>0)
            {
                for (int t=1; t<steps; t++)
                {
                    System.out.println("Point step "+t+" -> ("+a.x+velX*t+","+a.y+velY*t+")");
                }
            }
            System.out.println("Point b -> ("+b.x+","+b.y+")");
        }
    Output:
    Point a -> (0,0)
    Point step 1 -> (01.0,01.0)
    Point step 2 -> (02.0,02.0)
    Point step 3 -> (03.0,03.0)
    Point step 4 -> (04.0,04.0)
    Point b -> (5,5)

    Regards,

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points