public void EditPoint(int x, int y, Cairo.PointD offset) { int realX = x; int realY = y; if (!ConvertPointToCanvasPoint(offset, ref realX, ref realY)) { return; } if (listPoint == null) { listPoint = new List <BarierPoint>(); } BarierPoint bp = new BarierPoint(realX, realY); BarierPoint nearstBP = BarierPoint.ClosestPoint(bp, listPoint); double distance = 999; if (nearstBP != null) { distance = BarierPoint.Distance(bp, nearstBP); } if (distance < 5.5) { StartMovingPoint(x, y, offset); } else { AddPoint(x, y, offset); } }
public static double Distance(BarierPoint p1, BarierPoint p2) { double xDist = p1.X - p2.X; double yDist = p1.Y - p2.Y; return(Math.Sqrt(xDist * xDist + yDist * yDist)); }
public static int ClosestLine(BarierPoint p, IList <BarierPoint> points) { double shortestDistance = Double.PositiveInfinity; int indexOfPoint = -1; //foreach (var point in points) { for (int i = 0; i < points.Count; i++) { BarierPoint firstPoint = points[i]; BarierPoint secondPoint; if (i == points.Count - 1) { secondPoint = points[0]; } else { secondPoint = points[i + 1]; } double distance = FindDistanceToSegment(p, firstPoint, secondPoint); if (distance < shortestDistance) { shortestDistance = distance; //a = firstPoint; //b = secondPoint; indexOfPoint = i + 1; } } return(indexOfPoint); }
public void AddPoint(int x, int y, Cairo.PointD offset) { if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) { return; } if (listPoint == null) { listPoint = new List <BarierPoint>(); } BarierPoint bp = new BarierPoint(x, y); int indx = BarierPoint.ClosestLine(bp, listPoint); if (listPoint.Count > 8) { } if ((indx > listPoint.Count - 1) || (indx < 0)) { listPoint.Add(bp); } else { listPoint.Insert(indx, bp); } GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); }
public static BarierPoint ClosestPoint(BarierPoint p, IList<BarierPoint> points) { double shortestDistance = Double.PositiveInfinity; BarierPoint closestPoint = null; foreach (var point in points) { double distance = Distance(p, point); if (distance < shortestDistance) { shortestDistance = distance; closestPoint = point; } } return closestPoint; }
public static BarierPoint ClosestPoint(BarierPoint p, IList <BarierPoint> points) { double shortestDistance = Double.PositiveInfinity; BarierPoint closestPoint = null; foreach (var point in points) { double distance = Distance(p, point); if (distance < shortestDistance) { shortestDistance = distance; closestPoint = point; } } return(closestPoint); }
public override object Deserialize( IDictionary<string, object> dictionary,Type type,JavaScriptSerializer serializer ) { if( type == typeof( BarierPoint ) ) { var obj = new BarierPoint(); if( dictionary.ContainsKey( "x" ) ) obj.X = serializer.ConvertToType<int>( dictionary["x"] ); if( dictionary.ContainsKey( "y" ) ) obj.Y = serializer.ConvertToType<int>( dictionary["y"] ); return obj; } return null; }
public static BarierPoint LastPoint(IList <BarierPoint> points) { if (points == null || points.Count < 1) { return(null); } BarierPoint lastPoint = points[0]; foreach (var point in points) { if (lastPoint.Z < point.Z) { lastPoint = point; } } return(lastPoint); }
private static double FindDistanceToSegment(BarierPoint pt, BarierPoint p1, BarierPoint p2) //, out PointF closest) { BarierPoint closest = new BarierPoint(); float dx = p2.X - p1.X; float dy = p2.Y - p1.Y; if ((dx == 0) && (dy == 0)) { // It's a point not a line segment. closest = p1; dx = pt.X - p1.X; dy = pt.Y - p1.Y; return(Math.Sqrt(dx * dx + dy * dy)); } // Calculate the t that minimizes the distance. float t = ((pt.X - p1.X) * dx + (pt.Y - p1.Y) * dy) / (dx * dx + dy * dy); // See if this represents one of the segment's // end points or a point in the middle. if (t < 0) { closest = new BarierPoint(p1.X, p1.Y); dx = pt.X - p1.X; dy = pt.Y - p1.Y; } else if (t > 1) { closest = new BarierPoint(p2.X, p2.Y); dx = pt.X - p2.X; dy = pt.Y - p2.Y; } else { closest = new BarierPoint((int)(p1.X + t * dx), (int)(p1.Y + t * dy)); dx = pt.X - closest.X; dy = pt.Y - closest.Y; } return(Math.Sqrt(dx * dx + dy * dy)); }
public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) { if (type == typeof(BarierPoint)) { var obj = new BarierPoint(); if (dictionary.ContainsKey("x")) { obj.X = serializer.ConvertToType <int>( dictionary["x"]); } if (dictionary.ContainsKey("y")) { obj.Y = serializer.ConvertToType <int>( dictionary["y"]); } return(obj); } return(null); }
public void StartMovingPoint(int x, int y, Cairo.PointD offset) { if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) { return; } if (listPoint == null) { listPoint = new List <BarierPoint>(); } BarierPoint bp = new BarierPoint(x, y); BarierPoint nearstBP = BarierPoint.ClosestPoint(bp, listPoint); if (nearstBP != null) { movingPoint = nearstBP; //GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); } }
public void EndMovingPoint(int x, int y, Cairo.PointD offset) { if (movingPoint == null) { return; } if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) { movingPoint = null; return; } if (listPoint == null) { listPoint = new List <BarierPoint>(); } movingPoint.X = x; movingPoint.Y = y; movingPoint = null; GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); }
public static int ClosestLine(BarierPoint p, IList<BarierPoint> points ) { double shortestDistance = Double.PositiveInfinity; int indexOfPoint = -1; //foreach (var point in points) { for (int i = 0; i < points.Count; i++) { BarierPoint firstPoint = points[i]; BarierPoint secondPoint ; if(i== points.Count-1) secondPoint =points[0]; else secondPoint = points[i+1]; double distance =FindDistanceToSegment(p,firstPoint,secondPoint); if (distance < shortestDistance) { shortestDistance = distance; //a = firstPoint; //b = secondPoint; indexOfPoint = i+1; } } return indexOfPoint; }
//, out PointF closest) private static double FindDistanceToSegment(BarierPoint pt, BarierPoint p1, BarierPoint p2) { BarierPoint closest = new BarierPoint(); float dx = p2.X - p1.X; float dy = p2.Y - p1.Y; if ((dx == 0) && (dy == 0)) { // It's a point not a line segment. closest = p1; dx = pt.X - p1.X; dy = pt.Y - p1.Y; return Math.Sqrt(dx * dx + dy * dy); } // Calculate the t that minimizes the distance. float t = ((pt.X - p1.X) * dx + (pt.Y - p1.Y) * dy) / (dx * dx + dy * dy); // See if this represents one of the segment's // end points or a point in the middle. if (t < 0) { closest = new BarierPoint(p1.X, p1.Y); dx = pt.X - p1.X; dy = pt.Y - p1.Y; } else if (t > 1) { closest = new BarierPoint(p2.X, p2.Y); dx = pt.X - p2.X; dy = pt.Y - p2.Y; } else { closest = new BarierPoint((int)(p1.X + t * dx), (int)(p1.Y + t * dy)); dx = pt.X - closest.X; dy = pt.Y - closest.Y; } return Math.Sqrt(dx * dx + dy * dy); }
public static double Distance(BarierPoint p1, BarierPoint p2) { double xDist = p1.X - p2.X; double yDist = p1.Y - p2.Y; return Math.Sqrt(xDist * xDist + yDist * yDist); }
protected override bool OnExposeEvent(Gdk.EventExpose e) { Gdk.Pixbuf bg; try{ //if(fileName.ToLower().EndsWith (".svg")){ //bg = Rsvg.Pixbuf.FromFile(fileName); // bg = Rsvg.Tool.PixbufFromFileAtSize(fileName,800,600); //} else{ using (var fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open)) bg = new Gdk.Pixbuf(fs); //} }catch (Exception ex) { Tool.Logger.Error(ex.Message, null); return(true); } bg = bg.ApplyEmbeddedOrientation(); this.HeightImage = bg.Height; this.WidthImage = bg.Width; if (this.listPoint == null) { this.listPoint = new List <BarierPoint>(); if (this.shapeListPoint != null) { foreach (BarierPoint bp in this.shapeListPoint) { BarierPoint updateBp = new BarierPoint(); updateBp.X = bp.X + (1 / HeightImage); updateBp.Y = -bp.Y + (1 / WidthImage); listPoint.Add(updateBp); } } } //Size imagesize = new Size (bg.Width, bg.Height); width = (int)(bg.Width * scaling); height = (int)(bg.Height * scaling); int x, y, w, h, d = 0; this.ParentWindow.GetGeometry(out x, out y, out w, out h, out d); drawOffsetX = (w - width) / 2; if (drawOffsetX < 0) { drawOffsetX = 0; } drawOffsetY = (h - height) / 2; if (drawOffsetY < 0) { drawOffsetY = 0; } using (Context cr = Gdk.CairoHelper.Create(e.Window)) { if (!MainClass.Platform.IsMac) { FillChecks(cr, 0, 0, w, h); //w, h); cr.Save(); cr.DrawRectangle(new Cairo.Rectangle(drawOffsetX - 1, drawOffsetY - 1, width + 1, height + 1), new Cairo.Color(0, 0, 0), 1); cr.Rectangle(new Cairo.Rectangle(drawOffsetX - 1, drawOffsetY - 1, width, height)); cr.Clip(); } cr.Scale(scaling, scaling); CairoHelper.SetSourcePixbuf(cr, bg, drawOffsetX / scaling, drawOffsetY / scaling); cr.Paint(); this.WidthRequest = width + 1; this.HeightRequest = height + 1; if (showBarierLayer) { Draw(cr, width, height); } cr.Scale(scaling, scaling); } return(true); /*using (Context cr = Gdk.CairoHelper.Create (e.Window)) { * int w, h; * e.Window.GetSize (out w, out h); * Draw (cr, w, h); * } * return true;*/ }
protected override bool OnExposeEvent(Gdk.EventExpose e) { Gdk.Pixbuf bg; try{ //if(fileName.ToLower().EndsWith (".svg")){ //bg = Rsvg.Pixbuf.FromFile(fileName); // bg = Rsvg.Tool.PixbufFromFileAtSize(fileName,800,600); //} else{ using (var fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open)) bg = new Gdk.Pixbuf(fs); //} }catch(Exception ex){ Tool.Logger.Error(ex.Message,null); return true; } bg = bg.ApplyEmbeddedOrientation(); this.HeightImage = bg.Height; this.WidthImage= bg.Width; if ( this.listPoint == null ){ this.listPoint = new List<BarierPoint>(); if( this.shapeListPoint != null ){ foreach(BarierPoint bp in this.shapeListPoint){ BarierPoint updateBp = new BarierPoint(); updateBp.X = bp.X +(1/HeightImage); updateBp.Y = -bp.Y +(1/WidthImage); listPoint.Add(updateBp); } } } //Size imagesize = new Size (bg.Width, bg.Height); width = (int)(bg.Width * scaling); height = (int)(bg.Height * scaling); int x, y, w, h, d = 0; this.ParentWindow.GetGeometry(out x, out y, out w, out h, out d); drawOffsetX = (w - width) / 2; if (drawOffsetX < 0) drawOffsetX = 0; drawOffsetY = (h - height) / 2; if (drawOffsetY < 0) drawOffsetY = 0; using (Context cr = Gdk.CairoHelper.Create(e.Window)) { if(!MainClass.Platform.IsMac){ FillChecks (cr, 0, 0,w,h);//w, h); cr.Save (); cr.DrawRectangle(new Cairo.Rectangle(drawOffsetX - 1, drawOffsetY - 1, width + 1, height + 1), new Cairo.Color(0, 0, 0), 1); cr.Rectangle(new Cairo.Rectangle(drawOffsetX - 1, drawOffsetY - 1, width, height)); cr.Clip(); } cr.Scale(scaling, scaling); CairoHelper.SetSourcePixbuf(cr, bg, drawOffsetX / scaling, drawOffsetY / scaling); cr.Paint(); this.WidthRequest = width + 1; this.HeightRequest = height + 1; if (showBarierLayer) { Draw(cr, width, height); } cr.Scale(scaling, scaling); } return true; /*using (Context cr = Gdk.CairoHelper.Create (e.Window)) { int w, h; e.Window.GetSize (out w, out h); Draw (cr, w, h); } return true;*/ }
public void StartMovingPoint(int x, int y, Cairo.PointD offset) { if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) return; if (listPoint == null) listPoint = new List<BarierPoint>(); BarierPoint bp = new BarierPoint(x, y); BarierPoint nearstBP = BarierPoint.ClosestPoint(bp, listPoint); if (nearstBP != null) { movingPoint = nearstBP; //GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); } }
public void EndMovingPoint(int x, int y, Cairo.PointD offset) { if (movingPoint == null) return; if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) { movingPoint = null; return; } if (listPoint == null) listPoint = new List<BarierPoint>(); movingPoint.X = x; movingPoint.Y = y; movingPoint = null; GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); }
public void EditPoint(int x, int y, Cairo.PointD offset) { int realX = x; int realY = y; if (!ConvertPointToCanvasPoint(offset, ref realX, ref realY)) return; if (listPoint == null) listPoint = new List<BarierPoint>(); BarierPoint bp = new BarierPoint(realX, realY); BarierPoint nearstBP = BarierPoint.ClosestPoint(bp, listPoint); double distance = 999; if(nearstBP != null){ distance = BarierPoint.Distance(bp,nearstBP); } if(distance <5.5 ){ StartMovingPoint(x,y,offset); } else { AddPoint(x,y,offset); } }
public void AddPoint(int x, int y, Cairo.PointD offset) { if (!ConvertPointToCanvasPoint(offset, ref x, ref y)) return; if (listPoint == null) listPoint = new List<BarierPoint>(); BarierPoint bp = new BarierPoint(x, y); int indx = BarierPoint.ClosestLine(bp,listPoint); if(listPoint.Count >8){ } if((indx >listPoint.Count-1) || (indx<0)){ listPoint.Add(bp); } else { listPoint.Insert(indx,bp); } GdkWindow.InvalidateRect(new Gdk.Rectangle(drawOffsetX, drawOffsetY, width, height), false); }