English 中文(简体)
Partition line into equal parts
原标题:

This is a geometry question.

I have a line between two points A and B and want separate it into k equal parts. I need the coordinates of the points that partition the line between A and B.

Any help is highly appreciated.

Thanks a lot!

最佳回答

You just need a weighted average of A and B.

C(t) = A * (1-t) + B * t

or, in 2-D

Cx = Ax * (1-t) + Bx * t
Cy = Ay * (1-t) + By * t    
  • When t=0, you get A.
  • When t=1, you get B.
  • When t=.25, you a point 25% of the way from A to B
  • etc

So, to divide the line into k equal parts, make a loop and find C, for t=0/k, t=1/k, t=2/k, ... , t=k/k

问题回答
    for(int i=0;i<38;i++)
    {
        Points[i].x = m_Pos.x * (1 - (i/38.0)) + m_To.x * (i / 38.0);
        Points[i].y = m_Pos.y * (1 - (i/38.0)) + m_To.y * (i / 38.0);
        if(i == 0 || i == 37 || i == 19) dbg_msg("CLight","(%d)
Pos(%f,%f)
To(%f,%f)
Point(%f,%f)",i,m_Pos.x,m_Pos.y,m_To.x,m_To.y,Points[i].x,Points[i].y);
    }

prints:

[4c7cba40][CLight]: (0)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3376.000000,1808.000000)
[4c7cba40][CLight]: (19)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3388.425781,1767.357056)
[4c7cba40][CLight]: (37)
Pos(3376.000000,1808.000000)
To(3400.851563,1726.714111)
Point(3400.851563,1726.714111)

which looks fine but then my program doesn t work :D. but your method works so thanks





相关问题
Bounding ellipse

I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn t have to be axis aligned. This ...

Line segment in a triangle

How can we check if a line segment falls partially or fully inside a triangle? Cheers.

Line Segments from a point

I have a point p, and 2 line segments in a 2D plane. Point p is a location of view from where camera is looking towards the line segments. I want to check if line segment 1 is partially or fully ...

Creating a surface of triangles from a set of 2D points

I have a set of points and I need to convert the set to (non-overlapping) triangles (or a big polygon if equivalent)... The application: I have a list of locations (latitude,longitude) from a country,...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Delete holes in a Polygon

I have a polygon determined by an Array of Points. This polygon is crossing itself making some holes in the polygon itself. My questions is: How can I omit this holes and just get the exterior ...

Finding cycle of 3 nodes ( or triangles) in a graph

I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative ...

热门标签