/// <summary> /// Test whether object intersects with rectangle /// </summary> public override bool IntersectsWith(Rect rectangle) { RectangleGeometry rg = new RectangleGeometry(rectangle); LineGeometry lg = new LineGeometry(lineStart, lineEnd); PathGeometry widen = lg.GetWidenedPathGeometry(new Pen(Brushes.Black, LineHitTestWidth)); PathGeometry p = Geometry.Combine(rg, widen, GeometryCombineMode.Intersect, null); return(!p.IsEmpty()); }
void DrawLineCap(DrawingContext dc, Point point, Vector vector, PenLineCap startLineCap, PenLineCap endLineCap) { if (startLineCap == PenLineCap.Flat && endLineCap == PenLineCap.Flat) { return; } // Construct really tiny horizontal line vector.Normalize(); double angle = Math.Atan2(vector.Y, vector.X); RotateTransform rotate = new RotateTransform(-180 * angle / Math.PI, point.X, point.Y); Point point1 = rotate.Transform(point); Point point2 = rotate.Transform(point + 0.25 * vector); // Construct pen for that line Pen pen = new Pen() { Thickness = StrokeThickness, StartLineCap = startLineCap, EndLineCap = endLineCap }; pen.Freeze(); // Why don't I just call dc.DrawLine at this point? Well, to avoid gaps between // the tetragons, I had to draw them with an 'outlinePenWidth' pen based on the // same brush as the fill. If I just called dc.DrawLine here, the caps would // look a little smaller than the line, so.... LineGeometry lineGeo = new LineGeometry(point1, point2); PathGeometry pathGeo = lineGeo.GetWidenedPathGeometry(pen); Brush brush = null; if (GradientMode == GradientMode.Perpendicular) { brush = new LinearGradientBrush(GradientStops, new Point(0, 0), new Point(0, 1)); (brush as LinearGradientBrush).ColorInterpolationMode = ColorInterpolationMode; } else { double offset = endLineCap == PenLineCap.Flat ? 0 : 1; brush = new SolidColorBrush(GetColorFromGradientStops(offset)); brush.Freeze(); } pen = new Pen(brush, outlinePenWidth); pen.Freeze(); rotate.Angle = 180 * angle / Math.PI; dc.PushTransform(rotate); dc.DrawGeometry(brush, pen, pathGeo); dc.Pop(); }
private void DrawLineCap( DrawingContext dc, Line line, PenLineCap startLineCap, PenLineCap endLineCap) { if (startLineCap == PenLineCap.Flat && endLineCap == PenLineCap.Flat) { return; } var point = endLineCap == PenLineCap.Flat ? line.StartPoint : line.EndPoint; // Construct really tiny horizontal line var angle = Math.Atan2(line.Direction.Y, line.Direction.X); var rotate = new RotateTransform(-180 * angle / Math.PI, point.X, point.Y); var point1 = rotate.Transform(point); var point2 = rotate.Transform(point + 0.25 * line.Direction); // Construct pen for that line var pen = new Pen() { Thickness = this.StrokeThickness, StartLineCap = startLineCap, EndLineCap = endLineCap }; // Why don't I just call dc.DrawLine at this point? Well, to avoid gaps between // the tetragons, I had to draw them with an 'outlinePenWidth' pen based on the // same brush as the fill. If I just called dc.DrawLine here, the caps would // look a little smaller than the line, so.... var lineGeo = new LineGeometry(point1, point2); var pathGeo = lineGeo.GetWidenedPathGeometry(pen); Brush brush; if (this.GradientMode == GradientMode.Perpendicular) { brush = new LinearGradientBrush(this.GradientStops, new Point(0, 0), new Point(0, 1)); ((LinearGradientBrush)brush).ColorInterpolationMode = this.ColorInterpolationMode; } else { double offset = endLineCap == PenLineCap.Flat ? 0 : 1; brush = new SolidColorBrush(this.GradientStops.GetColorAt(offset, this.ColorInterpolationMode)); } pen = new Pen(brush, 0); rotate.Angle = 180 * angle / Math.PI; dc.PushTransform(rotate); dc.DrawGeometry(brush, pen, pathGeo); dc.Pop(); }
protected void CheckResizeLines(Point cursorPt) { // Determine if the cursor is over a line for resizing ResizeTopLine = new LineGeometry(_FocusedAnnotation.Marking.Rect.TopLeft, _FocusedAnnotation.Marking.Rect.TopRight); ResizeLeftLine = new LineGeometry(_FocusedAnnotation.Marking.Rect.TopLeft, _FocusedAnnotation.Marking.Rect.BottomLeft); ResizeBottomLine = new LineGeometry(_FocusedAnnotation.Marking.Rect.BottomLeft, _FocusedAnnotation.Marking.Rect.BottomRight); ResizeRightLine = new LineGeometry(_FocusedAnnotation.Marking.Rect.TopRight, _FocusedAnnotation.Marking.Rect.BottomRight); if (ResizeBottomLine.GetWidenedPathGeometry(_FocusedAnnotation.Pen).StrokeContains(_FocusedAnnotation.Pen, cursorPt)) { this.Cursor = System.Windows.Input.Cursors.SizeNS; ResizeTopLine = null; } else if (ResizeTopLine.GetWidenedPathGeometry(_FocusedAnnotation.Pen).StrokeContains(_FocusedAnnotation.Pen, cursorPt)) { this.Cursor = System.Windows.Input.Cursors.SizeNS; ResizeBottomLine = null; } else { ResizeBottomLine = null; ResizeTopLine = null; } if (ResizeRightLine.GetWidenedPathGeometry(_FocusedAnnotation.Pen).StrokeContains(_FocusedAnnotation.Pen, cursorPt)) { if (ResizeBottomLine != null) { this.Cursor = System.Windows.Input.Cursors.SizeNWSE; } else if (ResizeTopLine != null) { this.Cursor = System.Windows.Input.Cursors.SizeNESW; } else { this.Cursor = System.Windows.Input.Cursors.SizeWE; } ResizeLeftLine = null; } else if (ResizeLeftLine.GetWidenedPathGeometry(_FocusedAnnotation.Pen).StrokeContains(_FocusedAnnotation.Pen, cursorPt)) { if (ResizeBottomLine != null) { this.Cursor = System.Windows.Input.Cursors.SizeNESW; } else if (ResizeTopLine != null) { this.Cursor = System.Windows.Input.Cursors.SizeNWSE; } else { this.Cursor = System.Windows.Input.Cursors.SizeWE; } ResizeRightLine = null; } else { ResizeRightLine = null; ResizeLeftLine = null; } }