예제 #1
0
 public bool IsAbovePoint(PointF point)
 {
     Line2D line = new Line2D(point, 0.0f, 1.0f);
     PointF intersection = this.Intersection(line);
     if (intersection != PointF.Empty && intersection.Y > point.Y)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
예제 #2
0
 public float ValueAtX(float x)
 {
     Line2D line = new Line2D(new PointF(x, 0.0f), 0.0f, 1.0f);
     PointF intersection = this.Intersection(line);
     if (intersection != PointF.Empty)
     {
         return intersection.Y;
     }
     else
     {
         return float.NegativeInfinity;
     }
 }
예제 #3
0
 public Line2D GetNormalLine(PointF point)
 {
     PointF point2 = new PointF(point.X + VY, point.Y - VX);
     Line2D orthoLine = new Line2D(point, point2);
     return orthoLine;
 }
예제 #4
0
 public abstract PointF Intersection(Line2D line);
예제 #5
0
 public override PointF Intersection(Line2D line)
 {
     float k;
     k = line.VX * VY - line.VY * VX;
     if (k == 0) return PointF.Empty;
     k = (VY * (Point1.X - line.Point1.X) + VX * (line.Point1.Y - Point1.Y)) / k;
     return new PointF(line.Point1.X + k * line.VX, line.Point1.Y + k * line.VY);
 }
예제 #6
0
        public override float DistanceTo(PointF point)
        {
            if (this.Point1 == point || this.Point2 == point)
            {
                return 0;
            }

            // Calculate orhogonal line
            Line2D orthoLine = this.GetNormalLine(point);

            // Calculate Intersection
            PointF point3 = new Line2D(this.Point1, this.Point2).Intersection(orthoLine);

            if (this.ContainsAbsciss(point3.X))
            {
                // Calculate the length on the ortho segment
                Segment2D segment = new Segment2D(point, point3);
                return segment.Length();
            }
            else
            {
                Segment2D segment1 = new Segment2D(point, this.Point1);
                Segment2D segment2 = new Segment2D(point, this.Point2);
                return Math.Min(segment1.Length(), segment2.Length());
            }
        }
예제 #7
0
        public PointF Intersection(Segment2D segment)
        {
            Line2D line1 = new Line2D(this.Point1, this.Point2);
            Line2D line2 = new Line2D(segment.Point1, segment.Point2);
            PointF result = line1.Intersection(line2);

            if (result != PointF.Empty)
            {
                if (!new Rectangle2D(this.Point1, this.Point2).Contains(result) || !new Rectangle2D(segment.Point1, segment.Point2).Contains(result))
                {
                    result = PointF.Empty;
                }
            }
            return result;
        }
예제 #8
0
 public override PointF Intersection(Line2D line)
 {
     float k;
     k = line.VX * VY - line.VY * VX;
     if (k == 0) return PointF.Empty;
     k = (VY * (Point1.X - line.Point1.X) + VX * (line.Point1.Y - Point1.Y)) / k;
     PointF intersectionPoint = new PointF(line.Point1.X + k * line.VX, line.Point1.Y + k * line.VY);
     if (new Rectangle2D(this.Point1, this.Point2).Contains(intersectionPoint))
     {
         return intersectionPoint;
     }
     else
     {
         return PointF.Empty;
     }
 }
예제 #9
0
 private void MouseClickDrawing(System.Windows.Forms.MouseEventArgs e, ref PointF mousePoint, ref PointF mouseValuePoint)
 {
     switch (this.DrawingMode)
     {
         case GraphDrawMode.AddLine:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Selecting the first point
                     selectedValuePoint = mouseValuePoint;
                     this.DrawingStep = GraphDrawingStep.ItemSelected;
                     break;
                 case GraphDrawingStep.ItemSelected: // Selecting second point
                     PointF point1 = selectedValuePoint;
                     PointF point2 = mouseValuePoint;
                     try
                     {
                         Line2D newLine = new Line2D(point1, point2, this.DrawingPen);
                         drawingItems.Add(newLine);
                         AddToUndoBuffer(GraphActionType.AddItem, newLine);
                         this.DrawingStep = GraphDrawingStep.SelectItem;
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         selectedLineIndex = -1;
                     }
                     catch (System.ArithmeticException)
                     {
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.AddSegment:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Selecting the first point
                     selectedValuePoint = mouseValuePoint;
                     this.DrawingStep = GraphDrawingStep.ItemSelected;
                     break;
                 case GraphDrawingStep.ItemSelected: // Selecting second point
                     PointF point1 = selectedValuePoint;
                     PointF point2 = mouseValuePoint;
                     try
                     {
                         Segment2D newSegment = new Segment2D(point1, point2, this.DrawingPen);
                         drawingItems.Add(newSegment);
                         AddToUndoBuffer(GraphActionType.AddItem, newSegment);
                         this.DrawingStep = GraphDrawingStep.SelectItem;
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         selectedLineIndex = -1;
                     }
                     catch (System.ArithmeticException)
                     {
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.AddHalfLine:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Selecting the first point
                     selectedValuePoint = mouseValuePoint;
                     this.DrawingStep = GraphDrawingStep.ItemSelected;
                     break;
                 case GraphDrawingStep.ItemSelected: // Selecting second point
                     PointF point1 = selectedValuePoint;
                     PointF point2 = mouseValuePoint;
                     try
                     {
                         HalfLine2D newhalfLine = new HalfLine2D(point1, point2, this.DrawingPen);
                         drawingItems.Add(newhalfLine);
                         AddToUndoBuffer(GraphActionType.AddItem, newhalfLine);
                         this.DrawingStep = GraphDrawingStep.SelectItem;
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         selectedLineIndex = -1;
                     }
                     catch (System.ArithmeticException)
                     {
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.FanLine:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Selecting the first point
                     selectedValuePoint = mouseValuePoint;
                     this.DrawingStep = GraphDrawingStep.ItemSelected;
                     break;
                 case GraphDrawingStep.ItemSelected: // Selecting second point
                     PointF point1 = selectedValuePoint;
                     PointF point2 = mouseValuePoint;
                     try
                     {
                         Line2D newLine = (Line2D)new Line2D(point1, point2, this.DrawingPen);
                         drawingItems.Add(newLine);
                         AddToUndoBuffer(GraphActionType.AddItem, newLine);
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         selectedLineIndex = -1;
                     }
                     catch (System.ArithmeticException)
                     {
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.AndrewPitchFork:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Selecting the first point
                     if (andrewPitchFork == null)
                     {
                         andrewPitchFork = new AndrewPitchFork(mouseValuePoint, PointF.Empty, PointF.Empty);
                     }
                     else
                     {
                         // Selecting second point
                         andrewPitchFork.Point2 = mouseValuePoint;
                         this.DrawingStep = GraphDrawingStep.ItemSelected;
                     }
                     break;
                 case GraphDrawingStep.ItemSelected: // Selecting third point
                     andrewPitchFork.Point3 = mouseValuePoint;
                     try
                     {
                         foreach (Line2DBase newLine in andrewPitchFork.GetLines(this.DrawingPen))
                         {
                             drawingItems.Add(newLine);
                             AddToUndoBuffer(GraphActionType.AddItem, newLine);
                         }
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         selectedLineIndex = -1;
                         this.DrawingStep = GraphDrawingStep.SelectItem;
                         andrewPitchFork = null;
                     }
                     catch (System.ArithmeticException)
                     {
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.CopyLine:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Select the line to copy
                     selectedLineIndex = FindClosestLine(mousePoint);
                     if (selectedLineIndex != -1)
                     {
                         this.DrawingStep = GraphDrawingStep.ItemSelected;
                     }
                     break;
                 case GraphDrawingStep.ItemSelected: // Create new // line
                     if (selectedLineIndex != -1)
                     {
                         Line2D newLine = ((Line2DBase)this.drawingItems[selectedLineIndex]).GetParallelLine(mouseValuePoint);
                         newLine.Pen = this.DrawingPen;
                         drawingItems.Add(newLine);
                         AddToUndoBuffer(GraphActionType.AddItem, newLine);
                         this.DrawingStep = GraphDrawingStep.SelectItem;
                         selectedLineIndex = -1;
                         this.BackgroundDirty = true; // The new line becomes a part of the background
                         HighlightClosestLine(e);
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.DeleteItem:
             switch (this.DrawingStep)
             {
                 case GraphDrawingStep.SelectItem: // Delete element
                     selectedLineIndex = FindClosestLine(mousePoint);
                     if (selectedLineIndex != -1)
                     {
                         DrawingItem removeLine = this.drawingItems.ElementAt(selectedLineIndex);
                         this.drawingItems.RemoveAt(selectedLineIndex);
                         AddToUndoBuffer(GraphActionType.DeleteItem, removeLine);
                         this.BackgroundDirty = true; // New to redraw the background
                         HighlightClosestLine(e);
                     }
                     break;
                 default:   // Shouldn't come there
                     break;
             }
             break;
         case GraphDrawMode.CutLine:
             if (this.DrawingStep == GraphDrawingStep.SelectItem)
             {
                 selectedLineIndex = FindClosestLine(mousePoint);
                 if (selectedLineIndex != -1) // #### Check if visible to avoid cutting out of scope items
                 {
                     Line2DBase itemToCut = (Line2DBase)this.drawingItems.ElementAt(selectedLineIndex);
                     Line2DBase cutItem = itemToCut.Cut(mouseValuePoint.X, (Control.ModifierKeys & Keys.Control) == 0);
                     if (cutItem != null)
                     {
                         this.drawingItems.RemoveAt(selectedLineIndex);
                         this.drawingItems.Add(cutItem);
                         AddToUndoBuffer(GraphActionType.CutItem, itemToCut, cutItem);
                         this.BackgroundDirty = true; // New to redraw the background
                         HighlightCutLine(e, mouseValuePoint, (Control.ModifierKeys & Keys.Control) == 0);
                     }
                 }
             }
             break;
         default:
             break;
     }
 }