public static void ShowAngleBetween(Point pointOnline) { Line first = Form1.WrapperForSelectedLines; foreach (Line second in Form1._allLines) { if (second.PointOnLineSegment(pointOnline) && second != first) { LineEquintaince frstEQ = new LineEquintaince(first); LineEquintaince scdEQ = new LineEquintaince(second); double radAngle = LineEquintaince.AngleBetweenTwoLines(frstEQ, scdEQ); double degAngle = radAngle * (180 / Math.PI); string message = "Градусы: " + Math.Round(degAngle, 1) + "\nРадианы: " + Math.Round(radAngle, 3); string caption = "Угол между линиями"; MessageBoxButtons buttons = MessageBoxButtons.OK; DialogResult result; result = MessageBox.Show(message, caption, buttons); Form1.WrapperForSelectedLines.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } } }
public static double AngleBetweenTwoLines(LineEquintaince eq1, LineEquintaince eq2) { double cosN1N2 = ((eq1.A * eq2.A) + (eq1.B * eq2.B)) / (Math.Sqrt(Math.Pow(eq1.A, 2) + Math.Pow(eq1.B, 2)) * Math.Sqrt(Math.Pow(eq2.A, 2) + Math.Pow(eq2.B, 2))); return(Math.Acos(cosN1N2)); }
public static void AlignHorizontally(MouseEventArgs e) { foreach (Line currentLine in Form1._allLines) { if (currentLine.PointOnLineSegment(e.Location)) { if ( currentLine.LineType == Types.Single && !currentLine.Fixed ) { Line HORIZONTAL = new Line(-1, 0, 0); HORIZONTAL.StartPoint = new Point(0, 250); HORIZONTAL.EndPoint = new Point(930, 250); LineEquintaince modelLineEquainteince = new LineEquintaince(currentLine); LineEquintaince selectedLineEquainteince = new LineEquintaince(HORIZONTAL); double anglePHI; anglePHI = LineEquintaince.AngleBetweenTwoLines(selectedLineEquainteince, modelLineEquainteince); double angle = anglePHI * (180 / Math.PI); double dA = 180 - angle; if (dA > 0) { dA *= (-1); } if (currentLine.StartPoint.Y > currentLine.EndPoint.Y) { dA *= (-1); } double rotationAngle = -dA * Math.PI / 180; Point modifiedStart = new Point( (int)(-Math.Sin(rotationAngle) * (currentLine.StartPoint.Y - currentLine.LineCenter.Y) + Math.Cos(rotationAngle) * (currentLine.StartPoint.X - currentLine.LineCenter.X) + currentLine.LineCenter.X), //x1 (int)(Math.Cos(rotationAngle) * (currentLine.StartPoint.Y - currentLine.LineCenter.Y) + Math.Sin(rotationAngle) * (currentLine.StartPoint.X - currentLine.LineCenter.X) + currentLine.LineCenter.Y) //y1 ); Point modifiedEnd = new Point( (int)(-Math.Sin(rotationAngle) * (currentLine.EndPoint.Y - currentLine.LineCenter.Y) + Math.Cos(rotationAngle) * (currentLine.EndPoint.X - currentLine.LineCenter.X) + currentLine.LineCenter.X), //x2 (int)(Math.Cos(rotationAngle) * (currentLine.EndPoint.Y - currentLine.LineCenter.Y) + Math.Sin(rotationAngle) * (currentLine.EndPoint.X - currentLine.LineCenter.X) + currentLine.LineCenter.Y) //y2 ); currentLine.StartPoint = modifiedStart; currentLine.EndPoint = modifiedEnd; break; } } } }
public bool IsPerpendicularToLine(LineEquintaince lineEq) { if ((this.k * lineEq.k) >= -0.9 && (this.k * lineEq.k) <= -1.1) { return(true); } else if (double.IsInfinity(this.k) || double.IsInfinity(lineEq.K)) { return(true); } else { return(false); } }
public static void MakeParallelLine(Point pointOnLine) { Line lineToParallel = Form1.WrapperForSelectedLines; if (lineToParallel != null) { foreach (Line currentLine in Form1._allLines) { if (currentLine != lineToParallel) { if (currentLine.PointOnLineSegment(pointOnLine)) { LineEquintaince modelLineEquainteince = new LineEquintaince(currentLine); //ОРИЕНТИР ОРТОГАНАЛИЗАЦИИИ. Уравненеия конечно LineEquintaince selectedLineEquainteince = new LineEquintaince(lineToParallel); //ОБЪЕКТ ОРТОГАНАЛИЗАЦИИИ. Уравненеия конечно bool alreadyParallel = (modelLineEquainteince.K == selectedLineEquainteince.K); if (!alreadyParallel) { double anglePHI; double rotationAngle; anglePHI = LineEquintaince.AngleBetweenTwoLines(selectedLineEquainteince, modelLineEquainteince); double angle = anglePHI * (180 / Math.PI); //ttest double dA = 180 - angle; //if (dA < 0) //dA *= (-1); if (lineToParallel.EndPoint.Y < lineToParallel.StartPoint.Y) { dA *= (-1); } rotationAngle = dA * Math.PI / 180; if (lineToParallel.FixedPoint == FixedPointPosition.Center) { Point modifiedStart = new Point( (int)(-Math.Sin(rotationAngle) * (lineToParallel.StartPoint.Y - lineToParallel.LineCenter.Y) + Math.Cos(rotationAngle) * (lineToParallel.StartPoint.X - lineToParallel.LineCenter.X) + lineToParallel.LineCenter.X), //x1 (int)(Math.Cos(rotationAngle) * (lineToParallel.StartPoint.Y - lineToParallel.LineCenter.Y) + Math.Sin(rotationAngle) * (lineToParallel.StartPoint.X - lineToParallel.LineCenter.X) + lineToParallel.LineCenter.Y) //y1 ); Point modifiedEnd = new Point( (int)(-Math.Sin(rotationAngle) * (lineToParallel.EndPoint.Y - lineToParallel.LineCenter.Y) + Math.Cos(rotationAngle) * (lineToParallel.EndPoint.X - lineToParallel.LineCenter.X) + lineToParallel.LineCenter.X), //x2 (int)(Math.Cos(rotationAngle) * (lineToParallel.EndPoint.Y - lineToParallel.LineCenter.Y) + Math.Sin(rotationAngle) * (lineToParallel.EndPoint.X - lineToParallel.LineCenter.X) + lineToParallel.LineCenter.Y) //y2 ); lineToParallel.StartPoint = modifiedStart; lineToParallel.EndPoint = modifiedEnd; Form1.WrapperForSelectedLines.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } else { if (lineToParallel.FixedPoint == FixedPointPosition.Start) { Point modifiedEnd = new Point( (int)(-Math.Sin(rotationAngle) * (lineToParallel.EndPoint.Y - lineToParallel.StartPoint.Y) + Math.Cos(rotationAngle) * (lineToParallel.EndPoint.X - lineToParallel.StartPoint.X) + lineToParallel.StartPoint.X), //x2 (int)(Math.Cos(rotationAngle) * (lineToParallel.EndPoint.Y - lineToParallel.StartPoint.Y) + Math.Sin(rotationAngle) * (lineToParallel.EndPoint.X - lineToParallel.StartPoint.X) + lineToParallel.StartPoint.Y) //y2 ); lineToParallel.EndPoint = modifiedEnd; Form1.WrapperForSelectedLines.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } else if (lineToParallel.FixedPoint == FixedPointPosition.End) { Point modifiedStart = new Point( (int)(-Math.Sin(rotationAngle) * (lineToParallel.StartPoint.Y - lineToParallel.EndPoint.Y) + Math.Cos(rotationAngle) * (lineToParallel.StartPoint.X - lineToParallel.EndPoint.X) + lineToParallel.EndPoint.X), //x1 (int)(Math.Cos(rotationAngle) * (lineToParallel.StartPoint.Y - lineToParallel.EndPoint.Y) + Math.Sin(rotationAngle) * (lineToParallel.StartPoint.X - lineToParallel.EndPoint.X) + lineToParallel.EndPoint.Y) //y1 ); lineToParallel.StartPoint = modifiedStart; Form1.WrapperForSelectedLines.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } } } } } else { string message = "Не удается определить точку на линии."; string caption = "Ошибка."; MessageBoxButtons buttons = MessageBoxButtons.OK; DialogResult result; result = MessageBox.Show(message, caption, buttons); } } } }
public static void MakePerpendicularLine(Point pointOnLine) { Line lineToBeoRthogonal = Form1.WrapperForSelectedLines; if (lineToBeoRthogonal != null) //линия которая будет становиться перпендикулярной { foreach (Line currentLine in Form1._allLines) { if (currentLine != lineToBeoRthogonal) { if (currentLine.PointOnLineSegment(pointOnLine)) { LineEquintaince modelLineEquainteince = new LineEquintaince(currentLine); //ОРИЕНТИР ОРТОГАНАЛИЗАЦИИИ. Уравненеия конечно LineEquintaince selectedLineEquainteince = new LineEquintaince(lineToBeoRthogonal); //ОБЪЕКТ ОРТОГАНАЛИЗАЦИИИ. Уравненеия конечно bool alreadyPerpendicullary = modelLineEquainteince.IsPerpendicularToLine(selectedLineEquainteince); //Проверка, а вдруг уже перпенды if (!alreadyPerpendicullary) { double anglePHI; double rotationAngle; anglePHI = LineEquintaince.AngleBetweenTwoLines(selectedLineEquainteince, modelLineEquainteince); double angle = anglePHI * (180 / Math.PI); //ttest double dA = 90 - angle; if (lineToBeoRthogonal.EndPoint.Y > lineToBeoRthogonal.StartPoint.Y) { dA *= (-1); } rotationAngle = -dA * Math.PI / 180; if (lineToBeoRthogonal.FixedPoint == FixedPointPosition.Center) //если точку поворота не фиксирвали производить вращение относительно центра { Point modifiedStart = new Point( (int)(-Math.Sin(rotationAngle) * (lineToBeoRthogonal.StartPoint.Y - lineToBeoRthogonal.LineCenter.Y) + Math.Cos(rotationAngle) * (lineToBeoRthogonal.StartPoint.X - lineToBeoRthogonal.LineCenter.X) + lineToBeoRthogonal.LineCenter.X), //x1 (int)(Math.Cos(rotationAngle) * (lineToBeoRthogonal.StartPoint.Y - lineToBeoRthogonal.LineCenter.Y) + Math.Sin(rotationAngle) * (lineToBeoRthogonal.StartPoint.X - lineToBeoRthogonal.LineCenter.X) + lineToBeoRthogonal.LineCenter.Y) //y1 ); Point modifiedEnd = new Point( (int)(-Math.Sin(rotationAngle) * (lineToBeoRthogonal.EndPoint.Y - lineToBeoRthogonal.LineCenter.Y) + Math.Cos(rotationAngle) * (lineToBeoRthogonal.EndPoint.X - lineToBeoRthogonal.LineCenter.X) + lineToBeoRthogonal.LineCenter.X), //x2 (int)(Math.Cos(rotationAngle) * (lineToBeoRthogonal.EndPoint.Y - lineToBeoRthogonal.LineCenter.Y) + Math.Sin(rotationAngle) * (lineToBeoRthogonal.EndPoint.X - lineToBeoRthogonal.LineCenter.X) + lineToBeoRthogonal.LineCenter.Y) //y2 ); lineToBeoRthogonal.StartPoint = modifiedStart; lineToBeoRthogonal.EndPoint = modifiedEnd; lineToBeoRthogonal.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } //точка вращаения зафиксирована пользователем else { if (lineToBeoRthogonal.FixedPoint == FixedPointPosition.Start) //фиксирована начальная точка { Point modifiedEnd = new Point( (int)(-Math.Sin(rotationAngle) * (lineToBeoRthogonal.EndPoint.Y - lineToBeoRthogonal.StartPoint.Y) + Math.Cos(rotationAngle) * (lineToBeoRthogonal.EndPoint.X - lineToBeoRthogonal.StartPoint.X) + lineToBeoRthogonal.StartPoint.X), //x2 (int)(Math.Cos(rotationAngle) * (lineToBeoRthogonal.EndPoint.Y - lineToBeoRthogonal.LineCenter.Y) + Math.Sin(rotationAngle) * (lineToBeoRthogonal.EndPoint.X - lineToBeoRthogonal.StartPoint.X) + lineToBeoRthogonal.StartPoint.Y) //y2 ); lineToBeoRthogonal.EndPoint = modifiedEnd; lineToBeoRthogonal.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } else if (lineToBeoRthogonal.FixedPoint == FixedPointPosition.End) //фиксирована конечная точка { Point modifiedStart = new Point( (int)(-Math.Sin(rotationAngle) * (lineToBeoRthogonal.StartPoint.Y - lineToBeoRthogonal.EndPoint.Y) + Math.Cos(rotationAngle) * (lineToBeoRthogonal.StartPoint.X - lineToBeoRthogonal.EndPoint.X) + lineToBeoRthogonal.EndPoint.X), //x1 (int)(Math.Cos(rotationAngle) * (lineToBeoRthogonal.StartPoint.Y - lineToBeoRthogonal.EndPoint.Y) + Math.Sin(rotationAngle) * (lineToBeoRthogonal.StartPoint.X - lineToBeoRthogonal.EndPoint.X) + lineToBeoRthogonal.EndPoint.Y) //y1 ); lineToBeoRthogonal.StartPoint = modifiedStart; lineToBeoRthogonal.PenColor = Line.GENERAL_COLOR; Form1.WrapperForSelectedLines = null; break; } } } } } } } }