fill intersection of three or more sets in venn diagram - c#

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.

Related

Drawing smooth lines [duplicate]

I have a canvas and draw curve with this code:
using (Graphics g = Graphics.FromImage(canvas.BackgroundImage))
{
g.DrawCurve(pen, points);
points is array that I fill that by mouse location points.
In the result I see some jagged lines that I didn't draw.
You can see them here(in red rectangles):
What can i do about this?
What you is see is the somewhat unlucky combination of the default for Linejoin, which is Miter and the default for MiterLimit, which is 10.
Instead you have a choice of either picking one of the other LineJoin options or reducing the MiterLimit to say less than half the Pen.Width..
using (Pen myPen = new Pen(Color.Blue, 24f))
{
// either another LineJoine;
myPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
// or a reduced MiterLimit:
myPen.MiterLimit = 1+ myPen.Width / 5f;
}

How can I determine if a rectangle intersects a right angle triangle?

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:

Polygon Collision Testing / Polygon Overlap Test in C# - Not Point in Polygon

I am testing to determine if two polygons overlap. I have developed a first version which does a simple point in polygon test (Fig 1). However I am looking to revamp that method to deal with situations where no vertices of polygon A are in polygon B but their line segments overlap (Fig B).
Any help getting started would be greatly appreciated.
Here is an example with using Region:
GraphicsPath grp = new GraphicsPath();
// Create an open figure
grp.AddLine(10, 10, 10, 50); // a of polygon
grp.AddLine(10, 50, 50, 50); // b of polygon
grp.CloseFigure(); // close polygon
// Create a Region regarding to grp
Region reg = new Region(grp);
Now you can use the Method Region.IsVisible to determine whether the region is in an Rectangle or Point.
The solution:
I modified some code found here.
private Region FindIntersections(List<PolyRegion> regions)
{
if (regions.Count < 1) return null;
Region region = new Region();
for (int i = 0; i < regions.Count; i++)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddPath(regions[i].Path, false);
region.Intersect(path);
}
}
return region;
}
The result:

Finding difference of two plots in C#

I am having a laser scanner application where I want to find the difference between two plots ,one the reference plot without object and the other with the object in view.I am plotting the graph with x y coordinates. currently I have plotted the graphs and filled them with different colors so that I can view the subtracted part clearly. But now I want only the difference area to show up...I thought finding the area under the curve will solve the issue.But I think it will only give the numerical value and not the exact position of the subtracted area.
So,I searched the internet looking for solutions in C# where I can do this in the plot itself.Hope I made myself clear.
Can someone guide me in the search? I am giving my c# code here..
// PointPairList holds the data for plotting, X and Y arrays (one can use other types of objects as well)
PointPairList spl1 = new PointPairList(x1, y1);
PointPairList spl2 = new PointPairList(x2, y2);
PointPairList spl3 = new PointPairList(x, y);
// Add curves to myPane object
LineItem myCurve1 = myPane.AddCurve("LIDAR Data Scanner-Measurement-Normal", spl1, Color.Blue, SymbolType.None);
LineItem myCurve2 = myPane.AddCurve("LIDAR Data Scanner-Measurement-with object", spl2, Color.Red, SymbolType.None);
LineItem myCurve3 = myPane.AddCurve("LIDAR Data Scanner-Measurement-Subtracted curve", spl3, Color.Green, SymbolType.None);
// myCurve1.Line.Width = 3.0F;
//myCurve2.Line.Width = 3.0F;
myCurve1.Line.Fill = new Fill(Color.White, Color.FromArgb(16, 155, 0, 0), 90F);
myCurve2.Line.Fill = new Fill(Color.Black, Color.FromArgb(143, 55, 6, 0), 90F);
I want to display only the rectangle white part in the figure...
I am not sure about data-structures that you are sighted - however, generally speaking, if you are dealing with polygons (closed curves specifies with a set of x,y points) then you can do polygon clipping to find the difference. See
Algorithm to Compute the Remaining Polygon After Subtraction
How to intersect two polygons?
If you can represent your two plots i.e. a reference plot and supplied plot as polygon then above algorithm should allow you compute the difference.

Drawing an I-Beam based on two points

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.

Categories