我有一个由两个PointD定义的线段(一个自定义类型用于处理双精度而不是浮点数,因为C#中的数学在双精度数中起作用):p1(.541,41.929),p2(363.736,88.48) - 假设这条线会倾斜。
在不可预测的时间间隔内,我需要设置一个垂直于线段的偏移点,可变距离 - 只需说,0.125,0.25等。
似乎所有的例子都假设你总是需要这个垂直点基于中点。
我已经解决了p3 :(
private PointD subPt(PointD a, PointD b, double p)
{
double x = (1 - p) * a.X + p * b.X;
double y = (1 - p) * a.Y + p * b.Y;
return new PointD(x, y);
}
0.25的返回点是(91.339,53.566)。我已经在cad中验证了这一点,足以满足我的要求。
我无法弄清楚我的生活,就是如何在距离线条的位置得到一个垂直点5(只是为了使它成圆形)。
我期待的结果是p4(90.705,58.526)
答案 0 :(得分:0)
我已经提出了一个有效的C#实现:
/// <summary>
/// Create a perpendicular offset point at a position located along a line segment.
/// </summary>
/// <param name="a">Input. PointD(x,y) of p1.</param>
/// <param name="b">Input. PointD(x,y) of p2.</param>
/// <param name="position">Distance between p1(0.0) and p2 (1.0) in a percentage.</param>
/// <param name="offset">Distance from position at 90degrees to p1 and p2- non-percetange based.</param>
/// <param name="c">Output of the calculated point along p1 and p2. might not be necessary for the ultimate output.</param>
/// <param name="d">Output of the calculated offset point.</param>
private void PerpOffset(PointD a, PointD b, double position, double offset, out PointD c, out PointD d)
{
//p3 is located at the x or y delta * position + p1x or p1y original.
PointD p3 = new PointD(((b.X - a.X) * position) + a.X, ((b.Y - a.Y) * position) + a.Y);
//returns an angle in radians between p1 and p2 + 1.5708 (90degress).
double angleRadians = Math.Atan2(a.Y - b.Y, a.X - b.X) + 1.5708;
//locates p4 at the given angle and distance from p3.
PointD p4 = new PointD(p3.X + Math.Cos(angleRadians) * offset, p3.Y + Math.Sin(angleRadians) * offset);
//send out the calculated points
c = p3;
d = p4;
}
此外,这是我的PointD课程。基本上只是更容易存储和使用x,y值作为双精度,并包含一个属性来返回PointF用于图形输出目的。
public class PointD
{
public double X { get; set; }
public double Y { get; set; }
public float Xf { get { return (float)X; } }
public float Yf { get { return (float)Y; } }
public PointF PointF { get { return new PointF(Xf, Yf); } }
public PointD()
{ }
public PointD(double cX, double cY)
{
X = cX;
Y = cY;
}
public override string ToString()
{
return string.Format("[{0}, {1}]", X, Y);
}
}