开发者

Calculating distance using trigonometry in 3D

开发者 https://www.devze.com 2023-01-26 03:43 出处:网络
I\'m being quite dim - I can\'t figure out what should probably be a fairly trivial trig problem. Given cartesian coordinates (x, y, z), I would like to determine a new coordinate given a direction (

I'm being quite dim - I can't figure out what should probably be a fairly trivial trig problem.

Given cartesian coordinates (x, y, z), I would like to determine a new coordinate given a direction (x, y and z angles) and a distance to travel.

class Cartesian() {
  int x = 0;
  int y = 0;
  int z = 0;
  int move (int distance, int x_angle, int y_angle, int z_angle) {
    x += distance * //some trig here
    y += distance * //some trig here
    z += distance * //some trig here
  }
}

Ie, I want to move 开发者_如何学Ca given distance from the origin in a given direction, and need the coordinates of the new position.

This is actually for a JavaScript application, but I just need a bit of psuedocode to help me out.

Thanks


The way you've stated the problem, it seems that "direction cosines" make the most sense.

Assuming x_angle is the angle in radians between the target direction and the X axis, etc.:

dc_x = cos(x_angle);
dc_y = cos(y_angle);
dc_z = cos(z_angle);

delta_x = dc_x * distance;
delta_y = dc_y * distance;
delta_z = dc_z * distance;

x += delta_x;
y += delta_y;
z += delta_z;
0

精彩评论

暂无评论...
验证码 换一张
取 消