I have two points and I would like to compute n evenly distributed points on top of the line created by the given line. How could I perform this in c++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- ceil conterpart for Math.floorDiv in Java?
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Not so mcuh math though...
You can use the following
give_uniform_points_between(M, N, num_points)
which gives a number of#num_points
points betweenM
andN
. I assume here that the line is not vertical (see below if the line can be vertical).Demo : Live on Coliru
and result is :
Explanation
From two points
(x1,y1)
and(x2,y2)
you can guess the line equation which pass through these points.This equation takes the form
a*x + b*y + c = 0
or simplyy = a*x + b
if you cannot have vertical line wherea = (y2 - y1) / (x2 - x1)
and you deduce b as shown in the code.Then you can just vary
x
ory
along your line starting for the point with a minimum value coordinate.All these
(x,y)
points you find are on your line and should be uniformely distributed (thanks to the fixedstep
).Linear interpolation (affectionately called lerp by the Graphics community) is what you want. Given the end points it can generate the points lying in between with a parameter
t
.Let the end points be
A (Ax, Ay)
andB (Bx, By)
. The vector spanning fromA
toB
would be given byThis essentially means that starting from the point
A
, we scale the vectorV
with the scalart
; the pointA
is displaced by this scaled vector and thus the point we get depends on the value oft
, the parameter. Whent = 0
, we get backA
, ift = 1
we getB
, if it's0.5
we get the point midway betweenA
andB
.It works for any line (slope doesn't matter). Now coming to your problem of
N
stops. If you needN
to be 10, then you'd havet
vary by1/N
, sot = i/10
, wherei
would be the loop iterator.Here's one way to implement it:
Output
Aside
Notice that on every iteration
t
gets incremented byΔt = 1 / N
. Thus another way to updatet
in a loop would beHowever, this isn't very parallelizable since every iteration of the loop would depend on the previous iteration.
View the line as (x1,y1) + λ(x2-x1,y2-y1), i.e. the first point, plus a multiple of the vector between them.
When λ=0 you have the first point and when λ=1 you have the second. So you just want to take n equally distributed values of λ between 0 and 1.
How you do this depends on what you mean by between: are the end points included or not?
For example you could take λ=0/(n-1), λ=1/(n-1), λ=2/(n-1), ... λ=(n-1)/(n-1). That would give n equally distributed points including the endpoints.
Or you could take λ=1/(n+1), λ=2/(n+1), ... λ=n/(n+1). That would give n equally distributed points not including the endpoints.