我正在使用C#中的一个简单工具。我有三个点使两条线在P点相遇。所以PP1和PP2。我想在会合点处对线进行倒角,使得距线d1从线PP1修剪,距离d2从线PP2修剪,然后连接修剪线。 我有问题,因为我无法得到确切的结果。任何想法都是我的代码中的问题。 谢谢 Result1 Result2
private void Chamfer(Graphics g,PointF P,PointF P1,PointF P2,double d1,double d2)
{
//Vector 1 Length
double PP1 = Math.Sqrt((Math.Pow((P.X - P1.X), 2) + Math.Pow((P.Y - P1.Y), 2)));
//Vector 2 Length
double PP2 = Math.Sqrt((Math.Pow((P.X-P2.X), 2) + Math.Pow((P.Y - P2.Y), 2)));
//Slopes & Angles
double m1 = (P.Y-P1.Y) / (P.X - P1.X);
double angle1 = Math.Atan(m1) * (180 / Math.PI);
double m2 = (P.Y - P2.Y) / (P.X - P2.X);
double angle2 = Math.Atan(m2) * (180 / Math.PI);
//Coordinates of points of Chamfer
if(P1.X>P.X && P1.Y > P.Y)
{
int Pd1X = Convert.ToInt32(P.X + d1 * Math.Cos(angle1));
int Pd1Y = Convert.ToInt32(P.Y + d1 * Math.Sin(angle1));
Chp1 = new Point(Pd1X, Pd1Y);
}
else if (P1.X > P.X && P1.Y < P.Y)
{
int Pd1X = Convert.ToInt32(P.X + d1 * Math.Cos(angle1));
int Pd1Y = Convert.ToInt32(P.Y - d1 * Math.Sin(angle1));
Chp1 = new Point(Pd1X, Pd1Y);
}
else if (P1.X < P.X && P1.Y < P.Y)
{
int Pd1X = Convert.ToInt32(P.X - d1 * Math.Cos(angle1));
int Pd1Y = Convert.ToInt32(P.Y - d1 * Math.Sin(angle1));
Chp1 = new Point(Pd1X, Pd1Y);
}
else if (P1.X < P.X && P1.Y > P.Y)
{
int Pd1X = Convert.ToInt32(P.X - d1 * Math.Cos(angle1));
int Pd1Y = Convert.ToInt32(P.Y + d1 * Math.Sin(angle1));
Chp1 = new Point(Pd1X, Pd1Y);
}
if (P2.X > P.X && P2.Y > P.Y)
{
int Pd2X = Convert.ToInt32(P.X + d2 * Math.Cos(angle2));
int Pd2Y = Convert.ToInt32(P.Y + d2 * Math.Sin(angle2));
Chp2 = new Point(Pd2X, Pd2Y);
}
else if (P2.X > P.X && P2.Y < P.Y)
{
int Pd2X = Convert.ToInt32(P.X + d2 * Math.Cos(angle2));
int Pd2Y = Convert.ToInt32(P.Y - d2 * Math.Sin(angle2));
Chp2 = new Point(Pd2X, Pd2Y);
}
else if (P2.X < P.X && P2.Y < P.Y)
{
int Pd2X = Convert.ToInt32(P.X - d2 * Math.Cos(angle2));
int Pd2Y = Convert.ToInt32(P.Y - d2 * Math.Sin(angle2));
Chp2 = new Point(Pd2X, Pd2Y);
}
else if (P2.X < P.X && P2.Y > P.Y)
{
int Pd2X = Convert.ToInt32(P.X - d2 * Math.Cos(angle1));
int Pd2Y = Convert.ToInt32(P.Y + d2 * Math.Sin(angle1));
Chp2 = new Point(Pd2X, Pd2Y);
}
Pen penPre = new Pen(Color.Green);
penPre.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g.Clear(this.BackColor);
g.DrawLine(Pens.Black, P1, Chp1);
g.DrawLine(penPre, P1, P);
g.DrawLine(penPre, P2, P);
g.DrawString("P1", this.Font, Brushes.Red, new Point(Convert.ToInt32(P1.X + 2), Convert.ToInt32(P1.Y - 2)));
g.DrawLine(Pens.Black, P2, Chp2);
g.DrawString("P2", this.Font, Brushes.Red, new Point(Convert.ToInt32(P2.X + 2), Convert.ToInt32(P2.Y - 2)));
g.DrawString("P", this.Font, Brushes.Red, new Point(Convert.ToInt32( P.X + 3),Convert.ToInt32( P.Y - 2)));
g.DrawLine(Pens.Black, Chp1, Chp2);
}
我在特定坐标处获得的内容,我的代码正常工作,您可以从附带的照片中看到。如果我没有这些黄金坐标,我无法判断。 Result from golden coordinates
答案 0 :(得分:0)
//Vector 1 Length
double PP1 = Math.Sqrt((Math.Pow((P.X - P1.X), 2) + Math.Pow((P.Y - P1.Y), 2)));
//unit direction vector
upx1 = (P1.X - P.X) / PP1
upy1 = (P1.Y - P.Y) / PP1
// chpoint coordinates
chx1 = P.X + upx1 * d;
chy1 = P.Y + upy1 * d;
// now the same for PP2