Ejemplo n.º 1
0
 internal bool IsHit( Vector2DF mouse )
 {
     var size = new Vector2DF( Texture.Size.X, Texture.Size.Y ) * Scale;
     return mouse.X >= Position.X
         && mouse.X <= Position.X + size.X
         && mouse.Y >= Position.Y
         && mouse.Y <= Position.Y + size.Y;
 }
Ejemplo n.º 2
0
 public PointInfo(Vector2DF pt, bool isStartPt)
 {
     Point = pt;
     IsStartPoint = isStartPt;
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 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);
 }
Ejemplo n.º 6
0
 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
 }
Ejemplo n.º 7
0
 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
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 // *** 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;
 }