public override bool GroupRun(ref string message, ref CogToolResultConstants result) { // To let the execution stop in this script when a debugger is attached, uncomment the following lines. // #if DEBUG // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // #endif // Run each tool using the RunTool function foreach(ICogTool tool in Tools) RunTool(tool, ref message, ref result); // 输入距离 double a = this.Inputs.R/2; Point[] L1 = FindParallelLine(new Point(this.Inputs.LineSegment1.StartX,this.Inputs.LineSegment1.StartY), new Point(this.Inputs.LineSegment1.EndX,this.Inputs.LineSegment1.EndY), a); Point[] L2 = FindParallelLine(new Point(this.Inputs.LineSegment2.StartX,this.Inputs.LineSegment2.StartY), new Point(this.Inputs.LineSegment2.EndX,this.Inputs.LineSegment2.EndY), a); Point PX1 = GetIntersectionPoint(L1[0].X, L1[0].Y, L1[1].X, L1[1].Y, L2[0].X, L2[0].Y, L2[1].X, L2[1].Y); this.Outputs.X1 = PX1.X; this.Outputs.Y1 = PX1.Y; Point[] L3 = FindParallelLine(new Point(this.Inputs.LineSegment3.StartX, this.Inputs.LineSegment3.StartY), new Point(this.Inputs.LineSegment3.EndX, this.Inputs.LineSegment3.EndY), a); Point[] L4 = FindParallelLine(new Point(this.Inputs.LineSegment4.StartX, this.Inputs.LineSegment4.StartY), new Point(this.Inputs.LineSegment4.EndX, this.Inputs.LineSegment4.EndY), a); Point PX2 = GetIntersectionPoint(L3[0].X, L3[0].Y, L3[1].X, L3[1].Y, L4[0].X, L4[0].Y, L4[1].X, L4[1].Y); this.Outputs.X2 = PX2.X; this.Outputs.Y2 = PX2.Y; //输出值变量值除以1000 this.Outputs.D1 = (PX1.Y+a)/1000; this.Outputs.D2 = (PX2.Y+a)/1000; this.Outputs.DD = Math.Abs( this.Outputs.D1-this.Outputs.D2); return false; } //线段平移 public static Point[] FindParallelLine(Point start, Point end, double distance) { double length = Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2)); double dx = (end.X - start.X) / leng
时间: 2023-12-06 12:43:59 浏览: 135
th; double dy = (end.Y - start.Y) / length; double dx2 = dy * distance; double dy2 = -dx * distance; Point[] result = new Point[2]; result[0] = new Point(start.X + dx2, start.Y + dy2); result[1] = new Point(end.X + dx2, end.Y + dy2); return result; } //两线段交点 public static Point GetIntersectionPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double k1 = (y2 - y1) / (x2 - x1); double b1 = y1 - k1 * x1; double k2 = (y4 - y3) / (x4 - x3); double b2 = y3 - k2 * x3; double x = (b2 - b1) / (k1 - k2); double y = k1 * x + b1; return new Point(x, y); }
这是一段 C# 代码,实现了线段平移和两条线段的交点计算。其中,FindParallelLine函数实现了将一条线段沿着垂直方向平移一定距离后得到的新线段,GetIntersectionPoint函数实现了求两条线段的交点。
在GroupRun函数中,先通过FindParallelLine函数得到四条平行于输入线段的新线段,然后通过GetIntersectionPoint函数求出两两相交的交点,最后将交点的坐标作为输出值。
需要注意的是,在输出值计算时,需要将计算结果除以1000,因为输入的距离单位是毫米,而输出的距离单位是米。
阅读全文