简体   繁体   中英

Need help for algorithm to calculate position from current position like this (geometry 2D)

See image below:

illustration http://img28.imageshack.us/img28/5286/pic1we.png

Assume it's on android device screen. Dot #1 is the current position. I want to get the (x,y) position of Dot #2 following the linear blue line (let's say the range between dot #2 and dot #1 is 50dp). The black box is the source point of the blue line.

What is the term for this? Translation?

Is there anyone know the algorithm?

I developed android application using AndEngine, if you know there is built-in function for that, please let me know. Same too for android openGL ES library.

Note: I'm not looking for a way to move object using MoveModifier.

Thx in advance.

Authman's answer is good, but doesn't include how to get a specific distance away. If you're looking for p2 along the line, IMO it's easier to think about this in vectors. First you find the vector along the blue line, which is similar to the process that Authman describes above. If the black box has position v0 = (x0, y0) and the first dot has position v1 = (x1, y1) , then your "direction vector" is going to be v1 - v0 = (x1 - x0, y1 - y0) . This is a vector that points along the blue line. Once you have that, you normalize it so that the length is 1, and then multiply it by your desired distance, in your case 50. If you want to do it component-wise, here's some quick pseudocode:

find_v2(x0, y0, x1, y1):
  direction_x = x1 - x0
  direction_y = y1 - y0

  norm = sqrt(direction_x^2 + direction_y^2)
  direction_x *= 50 / norm
  direction_y *= 50 / norm

  x2 = x1 + direction_x
  y2 = y1 + direction_y

Hope that helps! I'm not sure how comfortable you are with vector math, but if you're doing graphics stuff, it can be really, really useful for problems like this; I'd recommend checking out some of the Khan Academy Linear Algebra courses .

The term you are looking for is extrapolation .

If the black dot is dot '0', then you first calculate your line equation's slope:

m = ( (d0.y-d1.y) / (d0.x-d1.x) )

Then you can extrapolate along that line, either positively (away from point 0) or negatively (towards it, until you get to it, and then eventually again away from in continuing in the line of motion).

dot2.x = d0.x + m*percentage
dot2.y = d0.y + m*percentage

Where percentage is how close you are to dot 1. Thus 1 (eg 100%) would put you on dot 1 exactly. percentage=0 would put you on dot 0 exactly. >1, would put you closer to dot 2. <1 would move you past dot 0, etc.

--

Slight correction there:

dot2.y = d0.x + m*percentage
dot2.x = (dot2.y - dot0.y) / m

The slope as i set it up is only useful for calculating the y coordinate of dot2. Since you already have the equation of the line, you can solve for dot2's x-coord directly using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM