I am trying to determine whether a rectangle intersects a triangle given the following triangle criteria:
the triangle will always be a right angle
the triangle will always derive from one of the corners of a rectangle
the triangle will always be the full width and height of the source rectangle
So one of my triangles can only be the top-left, top-right, bottom-left and bottom-right pieces of a rectangle.
Code would look similar to this:
Rectangle triangleSourceRect = new Rectangle(10, 10, 40, 40);
// top-left triangle from triangleSourceRect
Point[] topLeft = new Point[] {new Point(triangleSourceRect.Left,
triangleSourceRect.Top),
new Point(triangleSourceRect.Left,
triangleSourceRect.Bottom),
new Point(triangleSourceRect.Right,
triangleSourceRect.Top)};
Rectangle bounds = new Rectangle(28, 28, 20, 10);
if (bounds.Intersects(topLeft))
return true;
This is a diagram of the above code:
Related
i am writing the venn diagram and i have a problem in intersection.
i draw circles but i can't fill the intersection of 3 or more circles.
i fill intersection of two circles with this code
Graphics g = this.CreateGraphics();
GraphicsPath path = new GraphicsPath();
Region region1 = new Region();
Brush blue = new SolidBrush(Color.Blue);
Brush white=new SolidBrush(Color.White);
Rectangle circle1 = new Rectangle(455, 200, 150, 150);
Rectangle circle2 = new Rectangle(455, 275, 150, 150);
g.FillEllipse(blue, circle1);
g.FillEllipse(blue, circle2);
path.AddEllipse(circle1);
path.AddEllipse(circle2);
region1.Intersect(path);
g.FillRegion(white, region1);
i mean something like this
Right now you're trying to intersect an infinite region with a single GraphicsPath object containing both of your circles. Since the region is infinite to begin with, the Intersect method will just return the region occupied by the GraphicsPath object you specific.
To fix this, create your region by passing a GraphicsPath representing the 1st circle to the constructor. Then call the Intersect function using a different GraphicsPath containing the 2nd circle.
I am trying to draw a top view of an IC package, which should look like this (sorry I couldnt even draw it good enough using windows's paint!)
I am using a path obeject, but the result of my path object is no where near what I expect. Atleast the complete rectangle itself draws fine but I have problem to make that top arc you see in my example picture. Would be nice if you can point me to the right place. Here is my code:
private GraphicsPath DrawDilBounds(Size size)
{
var p = new GraphicsPath(FillMode.Alternate);
p.StartFigure();
p.AddLine(0, 0, 0, size.Height);
p.AddLine(0, size.Height, size.Width, size.Height);
p.AddLine(size.Width, size.Height, size.Width, 0);
p.AddLine(size.Width, 0, (size.Width/2) - 10, 0);
p.AddArc(size.Width/2 - 10, 0, 10, 10, 10, 10); //This arc looks like no arc!
p.AddLine((size.Width/2) + 10, 0, 0, 0);
p.CloseFigure();
return p;
}
So what I am doing here is starting some lines from top left corner , to bottom left corner, to right bottom corner and finaly to top right corner, then I added a line from top right corner to the middle of the top , minus 10 pixels then I want to add the arc with width of 20 pixels and then finish the drawing back to the top left corner.
You specify the arc by its bounding box. Using 10 as the radius gives a box of 20 x 20 (you used 10 x 10) whose upper left corner is located at (-10, -10) from the center of the arc (you used (-10, 0)). The last two arguments must be degrees, the starting and ending angle. Since you draw it from left-to-right that will be 0 and 180 degrees (you used 10 and 10). You also fumbled the lengths of the 2 lines at the top, they should be half the width -10 (you used +10). Fix:
p.AddLine(size.Width, 0, (size.Width / 2) + 10, 0);
p.AddArc(size.Width / 2 - 10, -10, 20, 20, 0, 180);
p.AddLine((size.Width / 2) - 10, 0, 0, 0);
Which gets you:
I have a Rect A inside an enclosing Rect B. the enclosing Rect B is the area in which the Rect A is allowed to be moved. when trying to move Rect A beyond the borders of enclosing Rect B it should get stuck at a border or corner of enclosing Rect B and move no further. while moving I have the following parameters at hand: properties of enclosing Rect B, properties of Rect A before the move, potential topleft position of Rect A after the move. Note that the move is not necessarily per-pixel, it might just as well be (for example) 20 pixels, in any direction. please tell me how do I go about doing this efficiently but not over-complicated?
PS: these are simply drawn geometries on a canvas in WPF so the use of transforms is also allowed but I only have Rect variables at this particular bit, not RectangleGeometries.
eventually I created an enclosing Rect of A and B, and then applied the solution in this question, like this:
private static Point CorrectForAllowedArea(Point previousLocation, Point newLocation, Rect allowedArea, Rect newBox)
{
// get area that encloses both rectangles
Rect enclosingRect = Rect.Union(allowedArea, newBox);
// get corners of outer rectangle, index matters for getting opposite corner
var outsideCorners = new[] { enclosingRect.TopLeft, enclosingRect.TopRight, enclosingRect.BottomRight, enclosingRect.BottomLeft }.ToList();
// get corners of inner rectangle
var insideCorners = new[] { allowedArea.TopLeft, allowedArea.TopRight, allowedArea.BottomRight, allowedArea.BottomLeft }.ToList();
// get the first found corner that both rectangles share
Point sharedCorner = outsideCorners.First((corner) => insideCorners.Contains(corner));
// find the index of the opposite corner
int oppositeCornerIndex = (outsideCorners.IndexOf(sharedCorner) + 2) % 4;
// calculate the displacement of the inside and outside opposite corner, this is the displacement outside the allowed area
Vector rectDisplacement = outsideCorners[oppositeCornerIndex] - insideCorners[oppositeCornerIndex];
// subtract that displacement from the total displacement that moved the shape outside the allowed area to get the displacement inside the allowed area
Vector allowedDisplacement = (newLocation - previousLocation) - rectDisplacement;
// use that displacement on the display location of the shape
return previousLocation + allowedDisplacement;
// move or resize the shape inside the allowed area, right upto the border, using the new returned location
}
I have two Point structures and I need to draw an I-Beam based on those points, where each point represents the cross-section on either side of the I-Beam. The width of the end caps should be fixed and arbitrary.
Basically I need to draw three lines. First I'll DrawLine(Point1, Point2), then I need the math to figure out how to draw the next two lines on perpendicular angles so that they are centered on Point1 and Point2.
The image below shows what I need to draw based on the center line. However, this line can be at any angle. The Point1 and Point2 that connect the line can be anywhere in a 2D space.
You can try playing around with LineCaps:
protected void DrawIBeam(Graphics g, Point fromPoint, Point toPoint)
{
using (GraphicsPath hPath = new GraphicsPath())
{
hPath.AddLine(new Point(-5, 0), new Point(5, 0));
CustomLineCap myCap = new CustomLineCap(null, hPath);
myCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
using (Pen myPen = new Pen(Color.Black, 2))
{
myPen.CustomStartCap = myCap;
myPen.CustomEndCap = myCap;
g.DrawLine(myPen, fromPoint, toPoint);
}
}
}
and call it:
DrawIBeam(e.Graphics, new Point(10, 10), new Point(60, 60));
From CustomLineCap Class
Assuming a width that's half the width of the I part of the I beam, first you find the slope of the first line you drew.
Next, you take the negative inverse of the slope, and draw a line from Point1 of length width in both directions. That's why width is half of the width you want to draw.
Finally you draw a line from Point 2 of length width in both directions.
Here's the mathematical formula for drawing a perpendicular line.
hello i am using c# and i want to find the if a polygon (mostly a triangle) is given to me and i have to find a given point exists in the given polygon or not i want to know that is there any function in c# that can do it for me or any efficient algo to do so??
polygons are represented in 2D plane by XY points the given point is also represented by XY points
thanx in advance.
You need to use Graphics.IsVisible(Point p).
It indicates whether the point specified by a pair of coordinates is contained within the visible clip region of this Graphics object.
Sample from MSDN :
public void IsVisiblePoint(PaintEventArgs e)
{
// Set clip region.
Region clipRegion = new Region(new Rectangle(50, 50, 100, 100));
e.Graphics.SetClip(clipRegion, CombineMode.Replace);
// Set up coordinates of points.
int x1 = 100;
int y1 = 100;
int x2 = 200;
int y2 = 200;
Point point1 = new Point(x1, y1);
Point point2 = new Point(x2, y2);
// If point is visible, fill ellipse that represents it.
if (e.Graphics.IsVisible(point1))
e.Graphics.FillEllipse(new SolidBrush(Color.Red), x1, y1, 10, 10);
if (e.Graphics.IsVisible(point2))
e.Graphics.FillEllipse(new SolidBrush(Color.Blue), x2, y2, 10, 10);
}
There are a couple of techniques to do this - the same side technique and the barycentric technique. Check out this link which explains the two in sufficient detail.
See http://en.wikipedia.org/wiki/Point_in_polygon for a first reference. As I mentioned in my comment, triangles would be much simpler.