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
Printable View
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
show us your code please,to see closely ,what is the problem
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.
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();
}
}
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);
}
}
Thanks a lot to all :)