public static bool IsObjectAt(float ptX, float ptY, TransformParams t, float x1, float y1, float x2, float y2, ref float dist) { Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); Vector2DF pt1 = t.Transform(new Vector2DF(x1, y1)); Vector2DF pt2 = t.Transform(new Vector2DF(x2, y2)); return(VisualizationUtils.TestLineHit(new Vector2DF(ptX, ptY), pt1, pt2, mHitDist, ref dist)); }
public override IDrawableObject GetObjectAt(float x, float y, TransformParams t, ref float dist) { Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); float width = t.Transform(mWidth); float height = t.Transform(mHeight); Vector2DF pos = t.Transform(new Vector2DF(mX - mWidth / 2f, mY - mHeight / 2f)); return(VisualizationUtils.PointInsideRect(x, y, new RectangleF(pos.X, pos.Y, width, height)) ? this : null); }
private static bool LineIntersectRectangle(Vector2DF pt1, Vector2DF pt2, RectangleF rect, ref Vector2DF isectPt1, ref Vector2DF isectPt2) { // note that this does not work properly in the case when the line lies on one of the rectangle's vertical edges // this is not a problem for the partial rendering of lines since the rectangles are inflated prior to determining // intersections float y = 0, x = 0; ArrayList <Vector2DF> points = new ArrayList <Vector2DF>(2); if (LineIntersectVertical(pt1, pt2, rect.X, ref y)) { if (y > rect.Y && y < rect.Y + rect.Height) { points.Add(new Vector2DF(rect.X, y)); } } if (LineIntersectVertical(pt1, pt2, rect.X + rect.Width, ref y)) { if (y > rect.Y && y < rect.Y + rect.Height) { points.Add(new Vector2DF(rect.X + rect.Width, y)); } } if (LineIntersectHorizontal(pt1, pt2, rect.Y, ref x)) { if (x > rect.X && x < rect.X + rect.Width) { points.Add(new Vector2DF(x, rect.Y)); } } if (LineIntersectHorizontal(pt1, pt2, rect.Y + rect.Height, ref x)) { if (x > rect.X && x < rect.X + rect.Width) { points.Add(new Vector2DF(x, rect.Y + rect.Height)); } } if (points.Count == 2) { isectPt1 = points[0]; isectPt2 = points[1]; return(true); } else if (points.Count == 1) { isectPt1 = points[0]; isectPt2 = VisualizationUtils.PointInsideRect(pt1.X, pt1.Y, rect) ? pt1 : pt2; return(true); } else if (VisualizationUtils.PointInsideRect(pt1.X, pt1.Y, rect) && VisualizationUtils.PointInsideRect(pt2.X, pt2.Y, rect)) { isectPt1 = pt1; isectPt2 = pt2; return(true); } return(false); }
public static bool IsObjectAt(Image image, float x, float y, float ptX, float ptY, TransformParams t) { Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); // TODO: other exceptions float width = t.Transform(image.Width); float height = t.Transform(image.Height); Vector2DF pos = t.Transform(new Vector2DF(x, y)); return(VisualizationUtils.PointInsideRect(ptX, ptY, new RectangleF(pos.X, pos.Y, width, height))); }
public static void Draw(float x1, float y1, float x2, float y2, Graphics g, Pen pen, TransformParams t) { Utils.ThrowException(g == null ? new ArgumentNullException("g") : null); Utils.ThrowException(pen == null ? new ArgumentNullException("pen") : null); Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); Vector2DF pt1 = t.Transform(new Vector2DF(x1, y1)); Vector2DF pt2 = t.Transform(new Vector2DF(x2, y2)); g.DrawLine(pen, pt1, pt2); }
public Vector2DF Transform(Vector2DF vec) { // scale vec.X *= mScaleFactor; vec.Y *= mScaleFactor; // translate vec.X += mTranslateX; vec.Y += mTranslateY; return(vec); }
public override void Draw(Graphics g, TransformParams t) { Utils.ThrowException(g == null ? new ArgumentNullException("g") : null); Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); Vector2DF pos = t.Transform(new Vector2DF(mX - mWidth / 2f, mY - mHeight / 2f)); using (Font font = new Font(mFont.FontFamily, t.Transform(mFont.Size), mFont.Style)) { g.DrawString(mLabel, font, mBrush, pos.X, pos.Y, StringFormat.GenericTypographic); } }
public static void Draw(Image image, float x, float y, Graphics g, TransformParams t) { Utils.ThrowException(g == null ? new ArgumentNullException("g") : null); Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); // TODO: throw other exceptions float width = t.Transform(image.Width); float height = t.Transform(image.Height); Vector2DF pos = t.Transform(new Vector2DF(x, y)); PixelOffsetMode pixelOffsetMode = g.PixelOffsetMode; g.PixelOffsetMode = PixelOffsetMode.Half; g.DrawImage(image, pos.X, pos.Y, width, height); g.PixelOffsetMode = pixelOffsetMode; }
public static void Draw(float x1, float y1, float x2, float y2, Graphics g, Pen pen, TransformParams t, BoundingArea.ReadOnly boundingArea) { #if !NO_PARTIAL_RENDERING Utils.ThrowException(g == null ? new ArgumentNullException("g") : null); Utils.ThrowException(pen == null ? new ArgumentNullException("pen") : null); Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); Utils.ThrowException(boundingArea == null ? new ArgumentNullException("boundingArea") : null); Vector2DF pt1 = t.Transform(new Vector2DF(x1, y1)); Vector2DF pt2 = t.Transform(new Vector2DF(x2, y2)); Vector2DF isectPt1 = new Vector2DF(); Vector2DF isectPt2 = new Vector2DF(); BoundingArea inflatedArea = boundingArea.GetWritableCopy(); inflatedArea.Inflate(pen.Width / 2f + 5f, pen.Width / 2f + 5f); ArrayList <KeyDat <float, PointInfo> > points = new ArrayList <KeyDat <float, PointInfo> >(); foreach (RectangleF rect in inflatedArea.Rectangles) { if (LineIntersectRectangle(pt1, pt2, rect, ref isectPt1, ref isectPt2)) { float distPt1 = (pt1 - isectPt1).GetLength(); float distPt2 = (pt1 - isectPt2).GetLength(); bool startPt1 = distPt1 < distPt2; points.Add(new KeyDat <float, PointInfo>(distPt1, new PointInfo(isectPt1, startPt1))); points.Add(new KeyDat <float, PointInfo>(distPt2, new PointInfo(isectPt2, !startPt1))); } } points.Sort(); int refCount = 0; int startIdx = 0; for (int i = 0; i < points.Count; i++) { PointInfo pointInfo = points[i].Dat; if (pointInfo.IsStartPoint) { refCount++; } else { refCount--; if (refCount == 0) { g.DrawLine(pen, points[startIdx].Dat.Point, pointInfo.Point); startIdx = i + 1; } } } #else Draw(x1, y1, x2, y2, g, pen, t); #endif }
public static void Draw(float x, float y, float rX, float rY, Graphics g, Pen pen, Brush brush, TransformParams t) { Utils.ThrowException(g == null ? new ArgumentNullException("g") : null); Utils.ThrowException(pen == null ? new ArgumentNullException("pen") : null); Utils.ThrowException(brush == null ? new ArgumentNullException("brush") : null); Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); x -= rX; y -= rY; Vector2DF center = t.Transform(new Vector2DF(x, y)); float dX = t.Transform(2f * rX); float dY = t.Transform(2f * rY); g.FillEllipse(brush, center.X, center.Y, dX, dY); g.DrawEllipse(pen, center.X, center.Y, dX, dY); }
public static bool IsObjectAt(float ptX, float ptY, TransformParams t, float cX, float cY, float rX, float rY) { Utils.ThrowException(t == null ? new ArgumentNullException("t") : null); Vector2DF center = t.Transform(new Vector2DF(cX, cY)); Vector2DF pt = new Vector2DF(ptX, ptY); if (pt == center) { return(true); } float angle = (pt - center).GetAngle(); float x = (float)Math.Cos(angle) * t.Transform(rX); float y = (float)Math.Sin(angle) * t.Transform(rY); float r = new Vector2DF(x, y).GetLength(); return((center - pt).GetLength() <= r); }
// *** Misc utils *** public static bool TestLineHit(Vector2DF testPt, Vector2DF lineTail, Vector2DF lineHead, float maxDist, ref float dist) { dist = float.MaxValue; if (lineTail != lineHead) { Vector2DF edge = lineHead - lineTail; Vector2DF edgeNormal = edge.Normal(); float intrsctX = 0, intrsctY = 0; float posA = 0, posB = 0; Vector2DF.Intersect(testPt, edgeNormal, lineTail, edge, ref intrsctX, ref intrsctY, ref posA, ref posB); if (posB >= 0f && posB <= 1f) { Vector2DF distVec = new Vector2DF(intrsctX, intrsctY) - testPt; dist = distVec.GetLength(); } dist = Math.Min((lineTail - testPt).GetLength(), dist); } dist = Math.Min((lineHead - testPt).GetLength(), dist); return(dist <= maxDist); }
private static bool LineIntersectVertical(Vector2DF pt1, Vector2DF pt2, float x, ref float y) { if (pt1.X > pt2.X) { Vector2DF tmp = pt1; pt1 = pt2; pt2 = tmp; } // swap points if (pt1.X < x && pt2.X > x) { float dY = pt2.Y - pt1.Y; if (dY == 0) { y = pt1.Y; return(true); } float dX = pt2.X - pt1.X; float dx = x - pt1.X; float dy = dx * dY / dX; y = pt1.Y + dy; return(true); } return(false); }
public static BoundingArea GetBoundingArea(float x1, float y1, float x2, float y2) { #if !SIMPLE_BOUNDING_AREA if (x1 == x2 || y1 == y2) { return(new BoundingArea(VisualizationUtils.CreateRectangle(x1, y1, x2, y2))); } float delta = Math.Abs((x2 - x1) / (y2 - y1)); float stepMax = (float)Math.Sqrt(mMaxBoxArea / delta + delta * mMaxBoxArea); Vector2DF line = new Vector2DF(x1, y1, x2, y2); float lineLen = line.GetLength(); if (stepMax >= lineLen) { return(new BoundingArea(VisualizationUtils.CreateRectangle(x1, y1, x2, y2))); } BoundingArea boundingArea = new BoundingArea(); int steps = (int)Math.Ceiling(lineLen / stepMax); Vector2DF stepVec = line; stepVec.SetLength(lineLen / (float)steps); Vector2DF pt1 = new Vector2DF(x1, y1); Vector2DF pt2; for (int i = 0; i < steps - 1; i++) { pt2 = pt1 + stepVec; boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(pt1.X, pt1.Y, pt2.X, pt2.Y)); pt1 = pt2; } pt2 = new Vector2DF(x2, y2); boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(pt1.X, pt1.Y, pt2.X, pt2.Y)); return(boundingArea); #else BoundingArea boundingArea = new BoundingArea(); boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(x1, y1, x2, y2)); return(boundingArea); #endif }
private static bool LineIntersectHorizontal(Vector2DF pt1, Vector2DF pt2, float y, ref float x) { return(LineIntersectVertical(new Vector2DF(pt1.Y, pt1.X), new Vector2DF(pt2.Y, pt2.X), y, ref x)); }
public PointInfo(Vector2DF pt, bool isStartPt) { Point = pt; IsStartPoint = isStartPt; }