예제 #1
0
 public void MoveNodes(UnitPoint position, IEnumerable<INodePoint> nodes)
 {
     if (m_undoBuffer.CanCapture)
         m_undoBuffer.AddCommand(new EditCommandNodeMove(nodes));
     foreach (INodePoint node in nodes)
     {
         node.SetPosition(position);
         node.Finish();
     }
 }
예제 #2
0
 public List<IDrawObject> GetHitObjects(ICanvas canvas, UnitPoint point)
 {
     List<IDrawObject> selected = new List<IDrawObject>();
     foreach (ICanvasLayer layer in m_layers)
     {
         if (layer.Visible == false)
             continue;
         foreach (IDrawObject drawobject in layer.Objects)
         {
             if (drawobject.PointInObject(canvas, point))
                 selected.Add(drawobject);
         }
     }
     return selected;
 }
예제 #3
0
 PointF Translate(UnitPoint point)
 {
     return(point.Point);
 }
예제 #4
0
 public bool HandleMouseDown(UnitPoint mouseunitpoint, ref bool handled)
 {
     handled = false;
     if (m_nodes.Count == 0) // no nodes selected yet
     {
         if (m_canvas.Model.SelectedCount > 0)
         {
             foreach (IDrawObject obj in m_canvas.Model.SelectedObjects)
             {
                 INodePoint p = obj.NodePoint(m_canvas, mouseunitpoint);
                 if (p != null)
                     m_nodes.Add(p);
             }
         }
         handled = m_nodes.Count > 0;
         if (handled)
             m_originPoint = mouseunitpoint;
         return handled;
     }
     // update selected nodes
     m_canvas.Model.MoveNodes(mouseunitpoint, m_nodes);
     m_nodes.Clear();
     handled = true;
     m_canvas.CanvasCtrl.DoInvalidate(true);
     return handled;
 }
예제 #5
0
 public PointF ToScreen(UnitPoint unitpoint)
 {
     return(m_canvas.ToScreen(unitpoint));
 }
예제 #6
0
 private static UnitPoint NearestPointOnLine(UnitPoint lp1, UnitPoint lp2, UnitPoint testpoint, double roundToAngleR)
 {
     if (lp1.X == testpoint.X)
     {
         UnitPoint tmp = lp1;
         lp1 = lp2;
         lp2 = tmp;
     }
     double A1 = LineAngleR(lp1, testpoint, 0);
     double A2 = LineAngleR(lp1, lp2, roundToAngleR);
     double A = A1 - A2;
     double h1 = (lp1.X - testpoint.X) / Math.Cos(A1);
     double h2 = Math.Cos(A) * h1;
     double x = Math.Cos(A2) * h2;
     double y = Math.Sin(A2) * h2;
     x = lp1.X - x;
     y = lp1.Y - y;
     return new UnitPoint((float)x, (float)y);
 }
예제 #7
0
        public static UnitPoint CenterPointFrom3Points(UnitPoint p1, UnitPoint p2, UnitPoint p3)
        {
            // http://local.wasp.uwa.edu.au/~pbourke/geometry/circlefrom3/
            // 3 points forms 2 lines a and b, slope of a is ma, b is mb
            // ma = (y2-y1) / (x2-x1)
            // mb = (y3-y2) / (x3-x2)
            double ma = (p2.Y - p1.Y) / (p2.X - p1.X);
            double mb = (p3.Y - p2.Y) / (p3.X - p2.X);

            // this is just a hacky work around for when p1-p2 are either horizontal or vertical.
            // A real solution should be found, but for now this will work
            if (double.IsInfinity(ma))
                ma = 1000000000000;
            if (double.IsInfinity(mb))
                mb = 1000000000000;
            if (ma == 0)
                ma = 0.000000000001;
            if (mb == 0f)
                mb = 0.000000000001;

            // centerpoint x = mamb(y1-y3) + mb(x1+x2) - ma(x2+x3) / 2(mb-ma)
            double center_x = (ma * mb * (p1.Y - p3.Y) + mb * (p1.X + p2.X) - ma * (p2.X + p3.X)) / (2 * (mb - ma));
            double center_y = (-1 * (center_x - (p1.X + p2.X) / 2) / ma) + ((p1.Y + p2.Y) / 2);

            //m_Center.m_y = -1*(m_Center.x() - (pt1->x()+pt2->x())/2)/aSlope +  (pt1->y()+pt2->y())/2;
            return new UnitPoint((float)center_x, (float)center_y);
        }
예제 #8
0
        public static bool IsPointInLine(UnitPoint linepoint1, UnitPoint linepoint2, UnitPoint testpoint, float halflinewidth)
        {
            UnitPoint p1 = linepoint1;
            UnitPoint p2 = linepoint2;
            UnitPoint p3 = testpoint;

            // check bounding rect, this is faster than creating a new rectangle and call r.Contains
            double lineLeftPoint  = Math.Min(p1.X, p2.X) - halflinewidth;
            double lineRightPoint = Math.Max(p1.X, p2.X) + halflinewidth;

            if (testpoint.X < lineLeftPoint || testpoint.X > lineRightPoint)
            {
                return(false);
            }

            double lineBottomPoint = Math.Min(p1.Y, p2.Y) - halflinewidth;
            double lineTopPoint    = Math.Max(p1.Y, p2.Y) + halflinewidth;

            if (testpoint.Y < lineBottomPoint || testpoint.Y > lineTopPoint)
            {
                return(false);
            }

            // then check if it hits the endpoint
            if (CircleHitPoint(p1, halflinewidth, p3))
            {
                return(true);
            }
            if (CircleHitPoint(p2, halflinewidth, p3))
            {
                return(true);
            }

            if (p1.Y == p2.Y) // line is horizontal
            {
                double min = Math.Min(p1.X, p2.X) - halflinewidth;
                double max = Math.Max(p1.X, p2.X) + halflinewidth;
                if (p3.X >= min && p3.X <= max)
                {
                    return(true);
                }
                return(false);
            }
            if (p1.X == p2.X) // line is vertical
            {
                double min = Math.Min(p1.Y, p2.Y) - halflinewidth;
                double max = Math.Max(p1.Y, p2.Y) + halflinewidth;
                if (p3.Y >= min && p3.Y <= max)
                {
                    return(true);
                }
                return(false);
            }

            // using COS law
            // a^2 = b^2 + c^2 - 2bc COS A
            // A = ACOS ((a^2 - b^2 - c^2) / (-2bc))
            double xdiff   = Math.Abs(p2.X - p3.X);
            double ydiff   = Math.Abs(p2.Y - p3.Y);
            double aSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double a       = Math.Sqrt(aSquare);

            xdiff = Math.Abs(p1.X - p2.X);
            ydiff = Math.Abs(p1.Y - p2.Y);
            double bSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double b       = Math.Sqrt(bSquare);

            xdiff = Math.Abs(p1.X - p3.X);
            ydiff = Math.Abs(p1.Y - p3.Y);
            double cSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double c       = Math.Sqrt(cSquare);
            double A       = Math.Acos(((aSquare - bSquare - cSquare) / (-2 * b * c)));

            // once we have A we can find the height (distance from the line)
            // SIN(A) = (h / c)
            // h = SIN(A) * c;
            double h = Math.Sin(A) * c;

            // now if height is smaller than half linewidth, the hitpoint is within the line
            return(h <= halflinewidth);
        }
예제 #9
0
 public static double Distance(UnitPoint p1, UnitPoint p2)
 {
     return(Distance(p1, p2, true));
 }
예제 #10
0
 public static UnitPoint OrthoPointD(UnitPoint lp1, UnitPoint lp2, double roundToAngleR)
 {
     return(OrthoPointR(lp1, lp2, DegressToRadians(roundToAngleR)));
 }
예제 #11
0
 public IDrawObject CreateObject(string type, UnitPoint point, ISnapPoint snappoint)
 {
     DrawingLayer layer = ActiveLayer as DrawingLayer;
     if (layer.Enabled == false)
         return null;
     DrawTools.DrawObjectBase newobj = CreateObject(type);
     if (newobj != null)
     {
         newobj.Layer = layer;
         newobj.InitializeFromModel(point, layer, snappoint);
     }
     return newobj as IDrawObject;
 }
예제 #12
0
        public static UnitPoint LinesIntersectPoint(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4)
        {
            double x = 0;
            double y = 0;

            if (LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, true, false, false))
            {
                return(new UnitPoint(x, y));
            }
            return(UnitPoint.Empty);
        }
예제 #13
0
        public static UnitPoint FindApparentIntersectPoint(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4, bool extendA, bool extendB)
        {
            double x = 0;
            double y = 0;

            if (LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, true, extendA, extendB))
            {
                return(new UnitPoint(x, y));
            }
            return(UnitPoint.Empty);
        }
예제 #14
0
        public static bool LinesIntersect(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4)
        {
            double x = 0;
            double y = 0;

            return(LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, false, false, false));
        }
예제 #15
0
        private static bool LinesIntersect(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4, ref double x, ref double y,
                                           bool returnpoint,
                                           bool extendA,
                                           bool extendB)
        {
            // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
            // line a is given by P1 and P2, point of intersect for line a (Pa) and b (Pb)
            // Pa = P1 + ua ( P2 - P1 )
            // Pb = P3 + ub ( P4 - P3 )

            // ua(x) = ub(x) and ua(y) = ub (y)
            // x1 + ua (x2 - x1) = x3 + ub (x4 - x3)
            // y1 + ua (y2 - y1) = y3 + ub (y4 - y3)

            // ua = ((x4-x3)(y1-y3) - (y4-y3)(x1-x3)) / ((x4-x3)(x2-x1) - (x4-x3)(y2-y1))
            // ub = ((x2-x1)(y1-y3) - (y2-y1)(x1-x3)) / ((y4-y3)(x2-x1) - (x4-x3)(y2-y1))

            // intersect point x = x1 + ua (x2 - x1)
            // intersect point y = y1 + ua (y2 - y1)
            double x1 = lp1.X;
            double x2 = lp2.X;
            double x3 = lp3.X;
            double x4 = lp4.X;
            double y1 = lp1.Y;
            double y2 = lp2.Y;
            double y3 = lp3.Y;
            double y4 = lp4.Y;

            double denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));

            if (denominator == 0) // lines are parallel
            {
                return(false);
            }
            double numerator_ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3));
            double numerator_ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3));
            double ua           = numerator_ua / denominator;
            double ub           = numerator_ub / denominator;

            // if a line is not extended then ua (or ub) must be between 0 and 1
            if (extendA == false)
            {
                if (ua < 0 || ua > 1)
                {
                    return(false);
                }
            }
            if (extendB == false)
            {
                if (ub < 0 || ub > 1)
                {
                    return(false);
                }
            }
            if (extendA || extendB) // no need to chck range of ua and ub if check is one on lines
            {
                x = x1 + ua * (x2 - x1);
                y = y1 + ua * (y2 - y1);
                return(true);
            }
            if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
            {
                if (returnpoint)
                {
                    x = x1 + ua * (x2 - x1);
                    y = y1 + ua * (y2 - y1);
                }
                return(true);
            }
            return(false);
        }
예제 #16
0
 static UnitPoint()
 {
     Empty     = new UnitPoint();
     Empty.m_x = double.NaN;
     Empty.m_y = double.NaN;
 }
예제 #17
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            /*
             * if (m_commandType == eCommandType.pan)
             * {
             *  m_panOffset.X += m_dragOffset.X;
             *  m_panOffset.Y += m_dragOffset.Y;
             *  m_dragOffset = new PointF(0, 0);
             * }
             */
            moving  = false;
            ispaste = false;
            List <IDrawObject> hitlist       = null;
            Rectangle          screenSelRect = Rectangle.Empty;

            if (m_selection != null)
            {
                screenSelRect = m_selection.ScreenRect();
                RectangleF selectionRect = m_selection.Selection(m_canvaswrapper);
                if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Eraser"))
                {
                    List <Line> l = CanvasCtrl.M_canvas.lines;
                    for (int i = l.Count - 1; i >= 0; i--)
                    {
                        if (l[i].P2.X <screenSelRect.Left || l[i].P1.X> screenSelRect.Right || l[i].P1.Y > screenSelRect.Bottom ||
                            l[i].P1.Y < screenSelRect.Top)
                        {
                        }
                        else
                        {
                            lines.RemoveAt(i);
                        }
                    }
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("chance color"))
                {
                    List <Line> l = CanvasCtrl.M_canvas.lines;
                    for (int i = l.Count - 1; i >= 0; i--)
                    {
                        if (l[i].P2.X <screenSelRect.Left || l[i].P1.X> screenSelRect.Right || l[i].P1.Y > screenSelRect.Bottom ||
                            l[i].P1.Y < screenSelRect.Top)
                        {
                        }
                        else
                        {
                            Line l1;
                            if (lines[i].isRed)
                            {
                                l1 = new Line(lines[i].P1, lines[i].P2, 0, false);
                            }
                            else
                            {
                                l1 = new Line(lines[i].P1, lines[i].P2, 0, true);
                            }
                            lines.RemoveAt(i);
                            lines.Add(l1);
                        }
                    }
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("move"))
                {
                    moving = true;                              //将移动状态设为正在移动
                    mr     = new MovingRegion(screenSelRect);   //新建移动范围
                    List <Line> selectedLines = mr.getLines();  //选中的线
                    lastY = e.Y;                                //设置初始纵轴位置
                    lastX = e.X;                                //设置初始横轴位置
                    List <Line> l = CanvasCtrl.M_canvas.lines;
                    for (int i = l.Count - 1; i >= 0; i--)
                    {
                        if (l[i].P2.X <screenSelRect.Left || l[i].P1.X> screenSelRect.Right || l[i].P1.Y > screenSelRect.Bottom ||
                            l[i].P1.Y < screenSelRect.Top)
                        {
                        }
                        else
                        {
                            Line l1 = new Line(l[i].P1, l[i].P2, l[i].S, l[i].isRed, l[i].Color);
                            selectedLines.Add(l1);
                            lines.Remove(l[i]);
                        }
                    }
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Cut"))
                {
                    pr = new MovingRegion(screenSelRect);       //新建移动范围
                    List <Line> selectedLines = pr.getLines();  //选中的线
                    lastY = e.Y;                                //设置初始纵轴位置
                    lastX = e.X;                                //设置初始横轴位置
                    List <Line> l = CanvasCtrl.M_canvas.lines;
                    for (int i = l.Count - 1; i >= 0; i--)
                    {
                        if (l[i].P2.X <screenSelRect.Left || l[i].P1.X> screenSelRect.Right || l[i].P1.Y > screenSelRect.Bottom ||
                            l[i].P1.Y < screenSelRect.Top)
                        {
                        }
                        else
                        {
                            Line l1 = new Line(l[i].P1, l[i].P2, l[i].S, l[i].isRed, l[i].Color);
                            selectedLines.Add(l1);
                            lines.Remove(l[i]);
                        }
                    }
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Copy"))
                {
                    pr = new MovingRegion(screenSelRect);       //新建移动范围
                    List <Line> selectedLines = pr.getLines();  //选中的线
                    lastY = e.Y;                                //设置初始纵轴位置
                    lastX = e.X;                                //设置初始横轴位置
                    List <Line> l = CanvasCtrl.M_canvas.lines;
                    for (int i = l.Count - 1; i >= 0; i--)
                    {
                        if (l[i].P2.X <screenSelRect.Left || l[i].P1.X> screenSelRect.Right || l[i].P1.Y > screenSelRect.Bottom ||
                            l[i].P1.Y < screenSelRect.Top)
                        {
                        }
                        else
                        {
                            Line l1 = new Line(l[i].P1, l[i].P2, l[i].S, l[i].isRed, l[i].Color);

                            selectedLines.Add(l1);
                        }
                    }
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Paste"))
                {
                    ispaste = true;
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Pai"))
                {
                    List <Line> l = CanvasCtrl.M_canvas.orlines;
                    if (e.Y >= 42 && e.Y < 177)
                    {
                        l.Add(new Line(new PointF(e.X, 40), new PointF(e.X, 179), Color.White, 5));
                    }
                    else if (e.Y >= 257 && e.Y < 393)
                    {
                        l.Add(new Line(new PointF(e.X, 255), new PointF(e.X, 395), Color.White, 5));
                    }
                    else if (e.Y >= 473 && e.Y < 609)
                    {
                        l.Add(new Line(new PointF(e.X, 470), new PointF(e.X, 608), Color.White, 5));
                    }
                    else if (e.Y >= 690 && e.Y < 820)
                    {
                        l.Add(new Line(new PointF(e.X, 688), new PointF(e.X, 822), Color.White, 5));
                    }

                    DoInvalidate(true);
                }
                else if (CanvasCtrl.M_canvas.m_drawObjectId.Equals("Jie"))
                {
                    List <Line> l = CanvasCtrl.M_canvas.orlines;
                    if (e.Y >= 42 && e.Y < 177)
                    {
                        l.Add(new Line(new PointF(e.X, 42), new PointF(e.X, 177), Color.Black, 2));
                    }
                    else if (e.Y >= 257 && e.Y < 393)
                    {
                        l.Add(new Line(new PointF(e.X, 257), new PointF(e.X, 393), Color.Black, 2));
                    }
                    else if (e.Y >= 473 && e.Y < 609)
                    {
                        l.Add(new Line(new PointF(e.X, 473), new PointF(e.X, 609), Color.Black, 2));
                    }
                    else if (e.Y >= 690 && e.Y < 820)
                    {
                        l.Add(new Line(new PointF(e.X, 690), new PointF(e.X, 820), Color.Black, 2));
                    }
                    DoInvalidate(true);
                }
                if (selectionRect != RectangleF.Empty)
                {
                    // is any selection rectangle. use it for selection
                    hitlist = m_model.GetHitObjects(m_canvaswrapper, selectionRect, m_selection.AnyPoint());
                    DoInvalidate(true);
                }
                else
                {
                    // else use mouse point
                    UnitPoint mousepoint = ToUnit(new PointF(e.X, e.Y));
                    hitlist = m_model.GetHitObjects(m_canvaswrapper, mousepoint);
                }
                m_selection = null;
            }

            if (m_commandType == eCommandType.draw && m_newObject != null)
            {
                UnitPoint mousepoint = ToUnit(m_mousedownPoint);
                if (m_snappoint != null)
                {
                    mousepoint = m_snappoint.SnapPoint;
                }
                m_newObject.OnMouseUp(m_canvaswrapper, mousepoint, m_snappoint);
            }
        }
예제 #18
0
 PointF Translate(UnitPoint point)
 {
     return point.Point;
 }
예제 #19
0
 public static UnitPoint OrthoPointR(UnitPoint lp1, UnitPoint lp2, double roundToAngleR)
 {
     return(NearestPointOnLine(lp1, lp2, lp2, roundToAngleR));
 }
예제 #20
0
        public static bool CircleIntersectWithLine(UnitPoint center, float radius, UnitPoint lp1, UnitPoint lp2)
        {
            // check if both points are inside the circle, in that case the line does not intersect the circle
            if ((Distance(center, lp1) < radius && Distance(center, lp2) < radius))
            {
                return(false);
            }

            // find the nearest point from the line to center, if that point is smaller than radius,
            // then the line does intersect the circle
            UnitPoint np   = NearestPointOnLine(lp1, lp2, center);
            double    dist = Distance(center, np);

            if (dist <= radius)
            {
                return(true);
            }
            return(false);
        }
예제 #21
0
 public PointF ToScreen(UnitPoint unitpoint)
 {
     return m_canvas.ToScreen(unitpoint);
 }
예제 #22
0
 public static UnitPoint NearestPointOnLine(UnitPoint lp1, UnitPoint lp2, UnitPoint tp)
 {
     return(NearestPointOnLine(lp1, lp2, tp, false));
 }
예제 #23
0
 public void DrawLine(ICanvas canvas, Pen pen, UnitPoint p1, UnitPoint p2)
 {
     PointF tmpp1 = ToScreen(p1);
     PointF tmpp2 = ToScreen(p2);
     //Console.WriteLine("pen:(" + pen.Color + "," + pen.Width + ")    tmpp1:(" + tmpp1.X + "," + tmpp1.Y + ")    tmpp2:(" + tmpp2.X + "," + tmpp2.Y+")");
     PointF tp = new PointF(tmpp2.X, tmpp1.Y);
     canvas.Graphics.DrawLine(pen, tmpp1, tp);
    
 }
예제 #24
0
 public static double LineSlope(UnitPoint p1, UnitPoint p2)
 {
     return((p2.Y - p1.Y) / (p2.X - p1.X));
 }
예제 #25
0
 public static UnitPoint NearestPointOnLine(UnitPoint lp1, UnitPoint lp2, UnitPoint tp, bool beyondSegment)
 {
     if (lp1.X == lp2.X)
     {
         // if both points are to below, then choose the points closest to tp.y
         if (beyondSegment == false && lp1.Y < tp.Y && lp2.Y < tp.Y)
             return new UnitPoint(lp1.X, Math.Max(lp1.Y, lp2.Y));
         // if both points are above, then choose the points closest to tp.y
         if (beyondSegment == false && lp1.Y > tp.Y && lp2.Y > tp.Y)
             return new UnitPoint(lp1.X, Math.Min(lp1.Y, lp2.Y));
         // else the line is on both sides and pick tp.x as y point
         return new UnitPoint(lp1.X, tp.Y);
     }
     if (lp1.Y == lp2.Y)
     {
         // if both points are to the right, then choose the points closest to tp.x
         if (beyondSegment == false && lp1.X < tp.X && lp2.X < tp.X)
             return new UnitPoint(Math.Max(lp1.X, lp2.X), lp1.Y);
         // if both points are to the left, then choose the points closest to tp.x
         if (beyondSegment == false && lp1.X > tp.X && lp2.X > tp.X)
             return new UnitPoint(Math.Min(lp1.X, lp2.X), lp1.Y);
         // else the line is on both sides and pick tp.x as x point
         return new UnitPoint(tp.X, lp1.Y);
     }
     return NearestPointOnLine(lp1, lp2, tp, 0);
 }
예제 #26
0
        public static bool IsPointInLine(UnitPoint linepoint1, UnitPoint linepoint2, UnitPoint testpoint, float halflinewidth)
        {
            UnitPoint p1 = linepoint1;
            UnitPoint p2 = linepoint2;
            UnitPoint p3 = testpoint;

            // check bounding rect, this is faster than creating a new rectangle and call r.Contains
            double lineLeftPoint = Math.Min(p1.X, p2.X) - halflinewidth;
            double lineRightPoint = Math.Max(p1.X, p2.X) + halflinewidth;
            if (testpoint.X < lineLeftPoint || testpoint.X > lineRightPoint)
                return false;

            double lineBottomPoint = Math.Min(p1.Y, p2.Y) - halflinewidth;
            double lineTopPoint = Math.Max(p1.Y, p2.Y) + halflinewidth;
            if (testpoint.Y < lineBottomPoint || testpoint.Y > lineTopPoint)
                return false;

            // then check if it hits the endpoint
            if (CircleHitPoint(p1, halflinewidth, p3))
                return true;
            if (CircleHitPoint(p2, halflinewidth, p3))
                return true;

            if (p1.Y == p2.Y) // line is horizontal
            {
                double min = Math.Min(p1.X, p2.X) - halflinewidth;
                double max = Math.Max(p1.X, p2.X) + halflinewidth;
                if (p3.X >= min && p3.X <= max)
                    return true;
                return false;
            }
            if (p1.X == p2.X) // line is vertical
            {
                double min = Math.Min(p1.Y, p2.Y) - halflinewidth;
                double max = Math.Max(p1.Y, p2.Y) + halflinewidth;
                if (p3.Y >= min && p3.Y <= max)
                    return true;
                return false;
            }

            // using COS law
            // a^2 = b^2 + c^2 - 2bc COS A
            // A = ACOS ((a^2 - b^2 - c^2) / (-2bc))
            double xdiff = Math.Abs(p2.X - p3.X);
            double ydiff = Math.Abs(p2.Y - p3.Y);
            double aSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double a = Math.Sqrt(aSquare);

            xdiff = Math.Abs(p1.X - p2.X);
            ydiff = Math.Abs(p1.Y - p2.Y);
            double bSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double b = Math.Sqrt(bSquare);

            xdiff = Math.Abs(p1.X - p3.X);
            ydiff = Math.Abs(p1.Y - p3.Y);
            double cSquare = Math.Pow(xdiff, 2) + Math.Pow(ydiff, 2);
            double c = Math.Sqrt(cSquare);
            double A = Math.Acos(((aSquare - bSquare - cSquare) / (-2 * b * c)));

            // once we have A we can find the height (distance from the line)
            // SIN(A) = (h / c)
            // h = SIN(A) * c;
            double h = Math.Sin(A) * c;

            // now if height is smaller than half linewidth, the hitpoint is within the line
            return h <= halflinewidth;
        }
예제 #27
0
 public static double LineSlope(UnitPoint p1, UnitPoint p2)
 {
     return (p2.Y - p1.Y) / (p2.X - p1.X);
 }
예제 #28
0
 static UnitPoint()
 {
     Empty = new UnitPoint();
     Empty.m_x = double.NaN;
     Empty.m_y = double.NaN;
 }
예제 #29
0
        public RectangleF HandleMouseMoveForNode(UnitPoint mouseunitpoint)
        {
            RectangleF r = RectangleF.Empty;
            if (m_nodes.Count == 0)
                return r;
            r = new RectangleF(m_originPoint.Point, new Size(0, 0));
            r = RectangleF.Union(r, new RectangleF(mouseunitpoint.Point, new SizeF(0, 0)));
            if (m_lastPoint != UnitPoint.Empty)
                r = RectangleF.Union(r, new RectangleF(m_lastPoint.Point, new SizeF(0, 0)));

            m_lastPoint = mouseunitpoint;
            foreach (INodePoint p in m_nodes)
            {
                if (r == RectangleF.Empty)
                    r = p.GetClone().GetBoundingRect(m_canvas);
                else
                    r = RectangleF.Union(r, p.GetClone().GetBoundingRect(m_canvas));
                p.SetPosition(mouseunitpoint);
                //m_canvas.RepaintObject(p.GetClone());
            }
            return r;
        }
예제 #30
0
        private static bool LinesIntersect(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4, ref double x, ref double y,
            bool returnpoint,
            bool extendA,
            bool extendB)
        {
            // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
            // line a is given by P1 and P2, point of intersect for line a (Pa) and b (Pb)
            // Pa = P1 + ua ( P2 - P1 )
            // Pb = P3 + ub ( P4 - P3 )

            // ua(x) = ub(x) and ua(y) = ub (y)
            // x1 + ua (x2 - x1) = x3 + ub (x4 - x3)
            // y1 + ua (y2 - y1) = y3 + ub (y4 - y3)

            // ua = ((x4-x3)(y1-y3) - (y4-y3)(x1-x3)) / ((x4-x3)(x2-x1) - (x4-x3)(y2-y1))
            // ub = ((x2-x1)(y1-y3) - (y2-y1)(x1-x3)) / ((y4-y3)(x2-x1) - (x4-x3)(y2-y1))

            // intersect point x = x1 + ua (x2 - x1)
            // intersect point y = y1 + ua (y2 - y1) 
            double x1 = lp1.X;
            double x2 = lp2.X;
            double x3 = lp3.X;
            double x4 = lp4.X;
            double y1 = lp1.Y;
            double y2 = lp2.Y;
            double y3 = lp3.Y;
            double y4 = lp4.Y;

            double denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
            if (denominator == 0) // lines are parallel
                return false;
            double numerator_ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3));
            double numerator_ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3));
            double ua = numerator_ua / denominator;
            double ub = numerator_ub / denominator;
            // if a line is not extended then ua (or ub) must be between 0 and 1
            if (extendA == false)
            {
                if (ua < 0 || ua > 1)
                    return false;
            }
            if (extendB == false)
            {
                if (ub < 0 || ub > 1)
                    return false;
            }
            if (extendA || extendB) // no need to chck range of ua and ub if check is one on lines 
            {
                x = x1 + ua * (x2 - x1);
                y = y1 + ua * (y2 - y1);
                return true;
            }
            if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
            {
                if (returnpoint)
                {
                    x = x1 + ua * (x2 - x1);
                    y = y1 + ua * (y2 - y1);
                }
                return true;
            }
            return false;
        }
예제 #31
0
 public ISnapPoint SnapPoint(ICanvas canvas, UnitPoint point, List<IDrawObject> otherobj)
 {
     foreach (IDrawObject obj in m_objects)
     {
         ISnapPoint sp = obj.SnapPoint(canvas, point, otherobj, null, null);
         if (sp != null)
             return sp;
     }
     return null;
 }
예제 #32
0
 public static bool LinesIntersect(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4)
 {
     double x = 0;
     double y = 0;
     return LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, false, false, false);
 }
예제 #33
0
 public void DrawLine(ICanvas canvas, Pen pen, UnitPoint p1, UnitPoint p2)
 {
     m_canvas.DrawLine(canvas, pen, p1, p2);
 }
예제 #34
0
 public static UnitPoint LinesIntersectPoint(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4)
 {
     double x = 0;
     double y = 0;
     if (LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, true, false, false))
         return new UnitPoint(x, y);
     return UnitPoint.Empty;
 }
예제 #35
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            m_mousedownPoint = new PointF(e.X, e.Y); // used when panning
            m_dragOffset     = new PointF(0, 0);

            //移动音符放下
            if (moving)
            {
                moving = false;
                lines.AddRange(mr.getLines());
            }
            if (ispaste)
            {
                ispaste = false;
                List <Line> l = pr.getLines();
                for (int i = 0; i < l.Count; i++)
                {
                    Line l1 = new Line(l[i].P1, l[i].P2, l[i].S, l[i].isRed, l[i].Color);
                    lines.Add(l1);
                }
            }
            if (m_drawObjectId.Equals("StartPoint"))
            {
                TestForm.setStartPoint(e.X, e.Y);
            }
            UnitPoint mousepoint = ToUnit(m_mousedownPoint);

            if (m_snappoint != null)
            {
                mousepoint = m_snappoint.SnapPoint;
            }

            if (m_commandType == eCommandType.select)
            {
                bool handled = false;
                if (m_nodeMoveHelper.HandleMouseDown(mousepoint, ref handled))
                {
                    m_commandType = eCommandType.editNode;
                    m_snappoint   = null;
                    base.OnMouseDown(e);
                    return;
                }
                m_selection = new SelectionRectangle(m_mousedownPoint);
            }

            /*
             * if (m_commandType == eCommandType.move)
             * {
             *  m_moveHelper.HandleMouseDownForMove(mousepoint, m_snappoint);
             * }
             * */
            if (m_commandType == eCommandType.draw)
            {
                HandleMouseDownWhenDrawing(mousepoint, null);
                DoInvalidate(true);
            }

            else if (m_commandType == eCommandType.symbol)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TestForm));
                CanvasWrapper canvastest = new CanvasWrapper(this, Graphics.FromImage(m_staticImage), ClientRectangle);
                canvastest.Graphics.SmoothingMode = m_smoothingMode;
                if (m_drawObjectId == "clef1" || m_drawObjectId == "clef2" || m_drawObjectId == "clef3" || m_drawObjectId == "clef4" || m_drawObjectId == "clef6" || m_drawObjectId == "N2" ||
                    m_drawObjectId == "N3" || m_drawObjectId == "N4" || m_drawObjectId == "N5" || m_drawObjectId == "N6" || m_drawObjectId == "N7" || m_drawObjectId == "N8" || m_drawObjectId == "N9" ||
                    m_drawObjectId == "N10" || m_drawObjectId == "N11" || m_drawObjectId == "N12" || m_drawObjectId == "s10" || m_drawObjectId == "s11" || m_drawObjectId == "s12" ||
                    m_drawObjectId == "N12" || m_drawObjectId == "N13")
                {
                    //  MessageBox.Show("test  test " + m_drawObjectId);

                    DrawSymbol(canvastest, e.X, e.Y, ((System.Drawing.Image)(resources.GetObject("ribbonButton_" + m_drawObjectId + ".Image"))), 1);
                }
                else if (m_drawObjectId == "bm1" || m_drawObjectId == "bm2" || m_drawObjectId == "bm3" || m_drawObjectId == "bm4" ||
                         m_drawObjectId == "bm5" || m_drawObjectId == "bm6" || m_drawObjectId == "bm7" || m_drawObjectId == "bm8" ||
                         m_drawObjectId == "bm18" || m_drawObjectId == "o11")
                {
                    DrawSymbol(canvastest, e.X, e.Y, ((System.Drawing.Image)(resources.GetObject("ribbonButton_" + m_drawObjectId + ".Image"))), 0.7);
                }
                else
                {
                    //  MessageBox.Show("test  test " + m_drawObjectId);
                    DrawSymbol(canvastest, e.X, e.Y, ((System.Drawing.Image)(resources.GetObject("ribbonButton_" + m_drawObjectId + ".Image"))), 0.4);
                }
                canvastest.CanvasCtrl.Refresh();
            }

            /*
             * if (m_commandType == eCommandType.edit)
             * {
             *  if (m_editTool == null)
             *      m_editTool = m_model.GetEditTool(m_editToolId);
             *  if (m_editTool != null)
             *  {
             *      if (m_editTool.SupportSelection)
             *          m_selection = new SelectionRectangle(m_mousedownPoint);
             *
             *      eDrawObjectMouseDown mouseresult = m_editTool.OnMouseDown(m_canvaswrapper, mousepoint, m_snappoint);
             * //                    /*
             * //                    if (mouseresult == eDrawObjectMouseDown.Continue)
             * //                    {
             * //                        if (m_editTool.SupportSelection)
             * //                            m_selection = new SelectionRectangle(m_mousedownPoint);
             * //                    }
             * //                     * * /
             * //
             *      if (mouseresult == eDrawObjectMouseDown.Done)
             *      {
             *          m_editTool.Finished();
             *          m_editTool = m_model.GetEditTool(m_editToolId); // continue with new tool
             *          //m_editTool = null;
             *
             *          if (m_editTool.SupportSelection)
             *              m_selection = new SelectionRectangle(m_mousedownPoint);
             *      }
             *  }
             *  DoInvalidate(true);
             *  UpdateCursor();
             * }
             */
            base.OnMouseDown(e);
        }
예제 #36
0
 public static UnitPoint FindApparentIntersectPoint(UnitPoint lp1, UnitPoint lp2, UnitPoint lp3, UnitPoint lp4, bool extendA, bool extendB)
 {
     double x = 0;
     double y = 0;
     if (LinesIntersect(lp1, lp2, lp3, lp4, ref x, ref y, true, extendA, extendB))
         return new UnitPoint(x, y);
     return UnitPoint.Empty;
 }
예제 #37
0
        public ISnapPoint SnapPoint(ICanvas canvas, UnitPoint point, Type[] runningsnaptypes, Type usersnaptype)
        {
            List<IDrawObject> objects = GetHitObjects(canvas, point);
            if (objects.Count == 0)
                return null;

            foreach (IDrawObject obj in objects)
            {
                ISnapPoint snap = obj.SnapPoint(canvas, point, objects, runningsnaptypes, usersnaptype);
                if (snap != null)
                    return snap;
            }
            return null;
        }
예제 #38
0
        public static bool LineIntersectWithRect(UnitPoint lp1, UnitPoint lp2, RectangleF r)
        {
            if (r.Contains(lp1.Point))
                return true;
            if (r.Contains(lp2.Point))
                return true;

            // the rectangle bottom is top in world units and top is bottom!, confused?
            // check left
            UnitPoint p3 = new UnitPoint(r.Left, r.Top);
            UnitPoint p4 = new UnitPoint(r.Left, r.Bottom);
            if (LinesIntersect(lp1, lp2, p3, p4))
                return true;
            // check bottom
            p4.Y = r.Top;
            p4.X = r.Right;
            if (LinesIntersect(lp1, lp2, p3, p4))
                return true;
            // check right
            p3.X = r.Right;
            p3.Y = r.Top;
            p4.X = r.Right;
            p4.Y = r.Bottom;
            if (LinesIntersect(lp1, lp2, p3, p4))
                return true;
            return false;
        }
예제 #39
0
 public static UnitPoint LineMidpoint(UnitPoint lp1, UnitPoint lp2)
 {
     UnitPoint mid = new UnitPoint();
     mid.X = (lp1.X + lp2.X) / 2;
     mid.Y = (lp1.Y + lp2.Y) / 2;
     return mid;
 }
예제 #40
0
 public static double LineAngleR(UnitPoint lp1, UnitPoint lp2, double roundToAngleR)
 {
     if (lp1.X == lp2.X)
     {
         if (lp1.Y > lp2.Y)
             return Math.PI * 6 / 4;
         if (lp1.Y < lp2.Y)
             return Math.PI / 2;
         return 0;
     }
     // first find angle A
     double adjacent = lp2.X - lp1.X;
     double opposite = lp2.Y - lp1.Y;
     // find angle A
     double A = Math.Atan(opposite / adjacent);
     if (adjacent < 0)			// 2nd and 3rd quadrant
         A += Math.PI;			// + 180
     if (adjacent > 0 && opposite < 0) // 4th quadrant
         A += Math.PI * 2;		// +360
     if (roundToAngleR != 0)
     {
         double roundUnit = Math.Round(A / roundToAngleR);
         A = roundToAngleR * roundUnit;
     }
     return A;
 }
예제 #41
0
 /*
 public PointF SnapPoint(PointF unitmousepoint)
 {
     return PointF.Empty;
 }
 public string Id
 {
     get { return "background"; }
 }
  * * * */
 ISnapPoint ICanvasLayer.SnapPoint(ICanvas canvas, UnitPoint point, List<IDrawObject> otherobj)
 {
     throw new Exception("The method or operation is not implemented.");
 }
예제 #42
0
 public static UnitPoint OrthoPointD(UnitPoint lp1, UnitPoint lp2, double roundToAngleR)
 {
     return OrthoPointR(lp1, lp2, DegressToRadians(roundToAngleR));
 }
예제 #43
0
 public void DrawLine(ICanvas canvas, Pen pen, UnitPoint p1, UnitPoint p2)
 {
     m_canvas.DrawLine(canvas, pen, p1, p2);
 }
예제 #44
0
 public static UnitPoint OrthoPointR(UnitPoint lp1, UnitPoint lp2, double roundToAngleR)
 {
     return NearestPointOnLine(lp1, lp2, lp2, roundToAngleR);
 }
예제 #45
0
        public PointF ToScreen(UnitPoint point)
        {
            PointF transformedPoint = Translate(point);
            transformedPoint.Y = ScreenHeight() - transformedPoint.Y;
            transformedPoint.Y *= m_screenResolution * m_model.Zoom;
            transformedPoint.X *= m_screenResolution * m_model.Zoom;

            transformedPoint.X += m_panOffset.X + m_dragOffset.X;
            transformedPoint.Y += m_panOffset.Y + m_dragOffset.Y;
            return transformedPoint;
        }
예제 #46
0
 public static UnitPoint NearestPointOnLine(UnitPoint lp1, UnitPoint lp2, UnitPoint tp)
 {
     return NearestPointOnLine(lp1, lp2, tp, false);
 }
예제 #47
0
 public virtual void HandleMouseDownWhenDrawing(UnitPoint mouseunitpoint, ISnapPoint snappoint)
 {
     if (m_commandType == eCommandType.draw)
     {
         if (m_newObject == null)
         {
             m_newObject = m_model.CreateObject(m_drawObjectId, mouseunitpoint, snappoint);
             DoInvalidate(false, m_newObject.GetBoundingRect(m_canvaswrapper));
         }
         else
         {
             if (m_newObject != null)
             {
                 eDrawObjectMouseDown result = m_newObject.OnMouseDown(m_canvaswrapper, mouseunitpoint, snappoint);
                 switch (result)
                 {
                     case eDrawObjectMouseDown.Done:
                         m_model.AddObject(m_model.ActiveLayer, m_newObject);
                         m_newObject = null;
                         DoInvalidate(true);
                         break;
                     case eDrawObjectMouseDown.DoneRepeat:
                         m_model.AddObject(m_model.ActiveLayer, m_newObject);
                         m_newObject = m_model.CreateObject(m_newObject.Id, m_newObject.RepeatStartingPoint, null);
                         DoInvalidate(true);
                         break;
                     case eDrawObjectMouseDown.Continue:
                         break;
                 }
             }
         }
     }
 }
예제 #48
0
 /*
  * public PointF SnapPoint(PointF unitmousepoint)
  * {
  *  return PointF.Empty;
  * }
  * public string Id
  * {
  *  get { return "background"; }
  * }
  * * * */
 ISnapPoint ICanvasLayer.SnapPoint(ICanvas canvas, UnitPoint point, List <IDrawObject> otherobj)
 {
     throw new Exception("The method or operation is not implemented.");
 }