public void GetAngleByHeight_Tests(double x1, double x2, double length, double angle) { DcLineSegment lineSegment = new DcLineSegment(new Point(x1, x2), length, angle); var actual = DcMath.GetAngleByHeight(lineSegment.Height, lineSegment.Length, lineSegment.X1, lineSegment.Y1, lineSegment.X2, lineSegment.Y2); Assert.AreEqual(lineSegment.Angle, actual, 0.0001); }
public void XoffsetByTan_Tests(double x1, double x2, double length, double angle, double dx) { DcLineSegment lineSegment = new DcLineSegment(new Point(x1, x2), length, angle); var actual = DcMath.XoffsetByTan(lineSegment.Height, lineSegment.Angle); Assert.AreEqual(dx, actual, 0.000001); }
/// <summary> /// /// </summary> /// <param name="originPoint"></param> /// <param name="length"></param> /// <param name="angle"></param> public DcLineSegment(Point originPoint, double length, double angle) { _x1 = originPoint.X; _y1 = originPoint.Y; while (angle >= 360) { angle -= 360; } while (angle <= -360) { angle += 360; } _angle = angle < 0 ? angle + 360 : angle; _length = length; if (angle == 0) { _x2 = _x1 + length; _y2 = _y1; } else if (angle == 180) { _x2 = _x1 - length; _y2 = _y1; } else if (angle == 90) { _x2 = _x1; _y2 = _y1 + length; } else if (angle == 270) { _x2 = _x1; _y2 = _y1 - length; } else { _x2 = _x1 + DcMath.Xoffset(length, angle); _y2 = _y1 + DcMath.Yoffset(length, angle); } _height = DcMath.GetHeight(Y1, Y2); _width = DcMath.GetWidth(X1, X2); _p1Hash = PointHash.CreateHash(1, _id); _p2Hash = PointHash.CreateHash(2, _id); _points = new Dictionary <int, Point>() { { _p1Hash, new Point(_x1, _y1) }, { _p2Hash, new Point(_x2, _y2) } }; }
/// <summary> /// /// </summary> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> public DcLineSegment(double x1, double y1, double x2, double y2) { _x1 = x1; _y1 = y1; _x2 = x2; _y2 = y2; _p1Hash = PointHash.CreateHash(1, _id); _p2Hash = PointHash.CreateHash(2, _id); _points = new Dictionary <int, Point>() { { _p1Hash, new Point(X1, Y1) }, { _p2Hash, new Point(X2, Y2) } }; _length = DcMath.GetDistance(X1, Y1, X2, Y2); _angle = DcMath.GetLineSegmentAngle(this); _height = DcMath.GetHeight(Y1, Y2); _width = DcMath.GetWidth(X1, X2); }
// Changes the angle of the DcLineSegment private void OnChangeAngle(double newAngle) { // Full tested while (newAngle >= 360) { newAngle -= 360; } while (newAngle <= -360) { newAngle += 360; } newAngle = newAngle < 0 ? newAngle + 360 : newAngle; // Tests if one of the point has a constraint. var p1HasConstraint = Owner.PointCollection[_p1Hash].ActiveHash != 0; var p2HasConstraint = Owner.PointCollection[_p2Hash].ActiveHash != 0; if (p2HasConstraint) { return; } else { if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Width)) { return; } else { // Tested if (newAngle == 0) { OnChangeP2(X1 + Length, Y1); _height = 0; _width = Length; } else if (newAngle == 180) { OnChangeP2(X1 - Length, Y1); _height = 0; _width = Length; } else if (newAngle == 90) { OnChangeP2(X1, Y1 + Length); _height = Length; _width = 0; } else if (newAngle == 270) { OnChangeP2(X1, Y1 - Length); _height = Length; _width = 0; } else { double xOffset = DcMath.Xoffset(Length, newAngle); double yOffset = DcMath.Yoffset(Length, newAngle); OnChangeP2(X1 + xOffset, Y1 + yOffset); } } } _angle = newAngle; }
// Changes the width of the DcLineSegment private void OnChangeWidth(double newWidth) { // Full tested if (newWidth <= 0 || newWidth == Width) { return; } if ((HasConstraint(Constraints.Length) && HasConstraint(Constraints.Angle)) || Angle == 90 || Angle == 270) { return; } // Tests if one of the point has a constraint. var p1HasConstraint = Owner.PointCollection[_p1Hash].ActiveHash != 0; var p2HasConstraint = Owner.PointCollection[_p2Hash].ActiveHash != 0; if (p1HasConstraint) { if (p2HasConstraint) { return; } else { double delta = (newWidth - Width); double dx = X2 - X1; delta = dx > 0 ? delta : -delta; if (HasConstraint(Constraints.Length)) { // Tested if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle)) { return; } else { double newHeight = DcMath.GetСathetus(newWidth, Length); double dy = Y2 - Y1; double yOffset = newHeight - Height; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP2(X2 + delta, Y2 + yOffset); _height = newHeight; _angle = DcMath.GetLineSegmentAngle(this); } } else if (HasConstraint(Constraints.Angle)) { // Tested if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Length)) { return; } else { double newHeigth = Math.Abs(DcMath.YoffsetByTan(newWidth, Angle)); double yOffset = newHeigth - Height; double dy = Y2 - Y1; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP2(X2 + delta, Y2 + yOffset); _length = DcMath.GetDistance(X1, Y1, X2, Y2); _height = newHeigth; } } else { // Tested OnChangeP2(X2 + delta, Y2); _length = DcMath.GetDistance(X1, Y1, X2, Y2); _angle = DcMath.GetLineSegmentAngle(this); } } } else { if (p2HasConstraint) { double delta = (newWidth - Width); double dx = X2 - X1; delta = dx > 0 ? delta : -delta; if (HasConstraint(Constraints.Length)) { // Tested if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle)) { return; } else { double newHeigth = DcMath.GetСathetus(newWidth, Length); double dy = Y2 - Y1; double yOffset = newHeigth - Height; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP1(X1 - delta, Y1 - yOffset); _height = newHeigth; _angle = DcMath.GetLineSegmentAngle(this); } } else if (HasConstraint(Constraints.Angle)) { // Tested if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Length)) { return; } else { double newHeight = Math.Abs(DcMath.YoffsetByTan(newWidth, Angle)); double yOffset = newHeight - Height; double dy = Y2 - Y1; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP1(X1 - delta, Y1 - yOffset); _length = DcMath.GetDistance(X1, Y1, X2, Y2); _height = newHeight; } } else { // Tested OnChangeP1(X1 - delta, Y1); _length = DcMath.GetDistance(X1, Y1, X2, Y2); _angle = DcMath.GetLineSegmentAngle(this); } } else { double delta = (newWidth - Width) / 2; double dx = X2 - X1; delta = dx > 0 ? delta : -delta; if (HasConstraint(Constraints.Length)) { if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle)) { return; } else { // Tested double newHeigth = DcMath.GetСathetus(newWidth, Length); double dy = Y2 - Y1; double yOffset = (newHeigth - Height) / 2; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP1(X1 - delta, Y1 - yOffset); OnChangeP2(X2 + delta, Y2 + yOffset); _height = newHeigth; _angle = DcMath.GetLineSegmentAngle(this); } } else if (HasConstraint(Constraints.Angle)) { // Tested if (HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Length)) { return; } else { double newHeigth = Math.Abs(DcMath.YoffsetByTan(newWidth, Angle)); double yOffset = (newHeigth - Height) / 2; double dY = Y2 - Y1; yOffset = dY > 0 ? yOffset : -yOffset; OnChangeP1(X1 - delta, Y1 - yOffset); OnChangeP2(X2 + delta, Y2 + yOffset); _height = newHeigth; _length = DcMath.GetDistance(X1, Y1, X2, Y2); } } else { // Tested OnChangeP1(X1 - delta, Y1); OnChangeP2(X2 + delta, Y2); _angle = DcMath.GetLineSegmentAngle(this); _length = DcMath.GetDistance(X1, Y1, X2, Y2); } } } AddLocalConstraint(Constraints.Width); _width = newWidth; }
// Changes the length of the DcLineSegment. private void OnChangeLength(double newLength) { // Full tested if (Length <= 0 || newLength == Length) { return; } // Tests if one of the point has a constraint. var p1HasConstraint = Owner.PointCollection[_p1Hash].ActiveHash != 0; var p2HasConstraint = Owner.PointCollection[_p2Hash].ActiveHash != 0; if (p1HasConstraint) { if (p2HasConstraint) { return; } else { if (HasConstraint(Constraints.Heigth)) { // Tested if (Angle == 90 || Angle == 270 || HasConstraint(Constraints.Width) || HasConstraint(Constraints.Angle) || newLength < Height) { return; } _angle = DcMath.GetAngleByHeight(Height, newLength, X1, Y1, X2, Y2); OnChangeP2(X1 + DcMath.Xoffset(newLength, _angle), Y2); } else if (HasConstraint(Constraints.Width)) { // Tested if (Angle == 0 || Angle == 180 || HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle) || newLength < Width) { return; } _angle = DcMath.GetAngleByWidth(Width, newLength, X1, Y1, X2, Y2); OnChangeP2(X2, Y1 + DcMath.Yoffset(newLength, _angle)); } else { // Tested if (newLength <= 0) { return; } double delta = newLength - _length; OnChangeP2(X2 + DcMath.Xoffset(delta, Angle), Y2 + DcMath.Yoffset(delta, Angle)); } } } else { if (p2HasConstraint) { if (HasConstraint(Constraints.Heigth)) { // Tested if (Angle == 90 || Angle == 270 || HasConstraint(Constraints.Width) || HasConstraint(Constraints.Angle) || newLength < Height) { return; } _angle = DcMath.GetAngleByHeight(Height, newLength, X1, Y1, X2, Y2); OnChangeP1(X2 - DcMath.Xoffset(newLength, _angle), Y1); } else if (HasConstraint(Constraints.Width)) { // Tested if (Angle == 0 || Angle == 180 || HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle) || newLength < Width) { return; } _angle = DcMath.GetAngleByWidth(Width, newLength, X1, Y1, X2, Y2); OnChangeP1(X1, Y2 - DcMath.Yoffset(newLength, _angle)); } else { // Tested if (newLength <= 0) { return; } double delta = newLength - _length; OnChangeP1(X1 - DcMath.Xoffset(delta, Angle), Y1 - DcMath.Yoffset(delta, Angle)); } } else { if (HasConstraint(Constraints.Heigth)) { // Tested if (Angle == 90 || Angle == 270 || HasConstraint(Constraints.Width) || HasConstraint(Constraints.Angle) || newLength < Height) { return; } double newWidth = DcMath.GetСathetus(Height, newLength); double dx = X2 - X1; double xOffset = (newWidth - Width) / 2; xOffset = dx > 0 ? xOffset : -xOffset; OnChangeP1(X1 - xOffset, Y1); OnChangeP2(X2 + xOffset, Y2); _angle = DcMath.GetLineSegmentAngle(this); } else if (HasConstraint(Constraints.Width)) { // Tested if (Angle == 0 || Angle == 180 || HasConstraint(Constraints.Heigth) || HasConstraint(Constraints.Angle) || newLength < Width) { return; } double newHeight = DcMath.GetСathetus(Width, newLength); double dy = Y2 - Y1; double yOffset = (newHeight - Height) / 2; yOffset = dy > 0 ? yOffset : -yOffset; OnChangeP1(X1, Y1 - yOffset); OnChangeP2(X2, Y2 + yOffset); _angle = DcMath.GetLineSegmentAngle(this); } else { // Tested double delta = newLength - _length; OnChangeP1(X1 - DcMath.Xoffset(delta / 2, Angle), Y1 - DcMath.Yoffset(delta / 2, Angle)); OnChangeP2(X2 + DcMath.Xoffset(delta / 2, Angle), Y2 + DcMath.Yoffset(delta / 2, Angle)); } } } _length = newLength; _width = DcMath.GetWidth(X1, X2); _height = DcMath.GetHeight(Y1, Y2); AddLocalConstraint(Constraints.Length); }