c# - Given a vector, calculate a point at distance l -


this extraordinarily simple question can't find answer i'm bracing myself ridicule.

given vector: enter image description here

(in case going 2,3 9,9) want start @ 2,3 , travel vector distance of length l.

how calculate new point (x,y)? thanks!

here's code (after implementing dmitry's equation):

 double metersmoved = ((dinosaurs[i].speed * 1000) / (60 * 60)) * timestepinsecs;  double fi = math.atan2((dinosaurs[i].goal.y - dinosaurs[i].head.y),(dinosaurs[i].goal.x - dinosaurs[i].head.x));  dinosaurs[i].head.x = dinosaurs[i].head.x + metersmoved * math.cos(fi);  dinosaurs[i].head.y = dinosaurs[i].head.y + metersmoved * math.sin(fi); 

the dinosaurs aligning on correct vector not advancing (one should moving 2 pixels , other 7).

dmitry's answer correct.

the solution (trigonometry) this:

  // origin point (2, 3)   double origin_x = 2.0;   double origin_y = 3.0;      // point moving toward   double to_x = 9.0;   double to_y = 9.0;    // let's travel 10 units    double distance = 10.0;    double fi = math.atan2(to_y - origin_y, to_x - origin_x);    // final point   double final_x = origin_x + distance * math.cos(fi);   double final_y = origin_y + distance * math.sin(fi); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -