Results 1 to 6 of 6
  1. #1
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default Calculate X and Y given starting angle and distance

    Hi all
    I am facing a problem calculating the destination X and Y coordinates of a point that moves to distance "A" from coordinate M and N at angle "theta".
    Can someone pls. help.

    Thanks
    Nidhi

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    show us your code please,to see closely ,what is the problem

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Sounds like you need a trig function. Like sin and cos
    You've got the hypotenuse("A") and angle(theta) and need to compute the x and y
    It's been a long time.

  4. #4
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default

    I am trying to simulate Random Walk where a mobile node is moving from one point(M,N) to another (X,Y) at a speed v and in time t at an angle "theta". distance = v*t. I need to find out how to find X,Y.
    The code I have written so far is :

    import java.awt.geom.Point2D;
    import java.util.*;

    public class RandomWalk
    {
    Point2D from;
    double direction;
    int speed = 10; // 10m/s already defined in Wei's paper
    double time;
    Point2D destination;
    RandomNumberGenerator generator = new RandomNumberGenerator();
    Vector angles = new Vector();

    public void getRandom() // randomly calculate an angle in radians and convert to angle
    {
    for(int i=0;i<1000;i++)
    {
    double angle = Math.toDegrees(generator.irand(0,360));
    angles.addElement(angle);
    }
    }

    public void getdestinationCoord(Point2D ptd)
    {
    // (r*cos(phi), r*sin(phi))
    }

    public double distance(Point2D pt) {
    return destination.distance(pt);
    }

    public void setLocation(Point2D p) {
    destination.setLocation(p);
    }

    public RandomWalk(Point2D from, double direction, int speed, double time)
    {
    super();
    this.from = from;
    this.direction = direction;
    this.speed = speed;
    this.time = time;
    }

    public double distanceSq(Point2D pt)
    {
    return from.distanceSq(pt);
    }

    public String toString()
    {
    return from.toString();
    }



    }

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class TrigTest extends JPanel {
        Point2D.Double center = new Point2D.Double(200, 175);
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            g2.fill(new Ellipse2D.Double(center.x-2, center.y-2, 4, 4));
            g2.setPaint(Color.green.darker());
            Point2D.Double p = getPoint(-45.0, 200.0);
            g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
            g2.setPaint(Color.red);
            p = getPoint(60.0, 150.0);
            g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
        }
    
        private Point2D.Double getPoint(double angle, double distance) {
            // Angles in java are measured clockwise from 3 o'clock.
            double theta = Math.toRadians(angle);
            Point2D.Double p = new Point2D.Double();
            p.x = center.x + distance*Math.cos(theta);
            p.y = center.y + distance*Math.sin(theta);
            return p;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new TrigTest());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  6. #6
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-14-2009, 10:01 PM
  2. Replies: 0
    Last Post: 07-27-2008, 07:56 PM
  3. just starting
    By specbailey in forum New To Java
    Replies: 23
    Last Post: 08-13-2007, 11:25 PM
  4. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:03 AM
  5. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 06:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •