How to join overlapping circles? - c#

I want to visually join two circles that are overlapping so that
becomes
I already have methods for partial circles, but now I need to know how large the overlapping angle for earch circle is, and I don't know how to do that.
Anyone got an Idea?

Phi= ArcTan[ Sqrt[4 * R^2 - d^2] /d ]
HTH!
Edit
For two different radii:
Simplifying a little:
Phi= ArcTan[Sqrt[-d^4 -(R1^2 - R2^2)^2 + 2*d^2*(R1^2 + R2^2)]/(d^2 +R1^2 -R2^2)]
Edit
If you want the angle viewed from the other circle center, just exchange R1 by R2 in the last equation.
Here is a sample implementation in Mathematica:
f[center1_, d_, R1_, R2_] := Module[{Phi, Theta},
Phi= ArcTan[Sqrt[-d^4-(R1^2-R2^2)^2 + 2*d^2*(R1^2 + R2^2)]/(d^2 +R1^2 -R2^2)]
Theta=ArcTan[Sqrt[-d^4-(R1^2-R2^2)^2 + 2*d^2*(R1^2 + R2^2)]/(d^2 -R1^2 +R2^2)]
{Circle[{center1, 0}, R1, {2 Pi - Phi, Phi}],
Circle[{d, 0}, R2, {Pi - Theta, -Pi + Theta}]}
];
Graphics[f[0, 1.5, 1, 1]]
Graphics[f[0, 1.5, 1, 3/4]]
And...
ImageMultiply[
Binarize#FillingTransform[#],
ImageResize[Import#
"http://i305.photobucket.com/albums/nn235/greeneyedgirlox/blondebabybunny.jpg",
ImageDimensions##]] &#
Rasterize#Graphics[f[0, 1.5, 1, 1], Background -> Black]
:)

Now this will work 100% for you even the figure is ellipse and any number of figures
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red, 2);
Rectangle Fig1 = new Rectangle(50, 50, 100, 50); //dimensions of Fig1
Rectangle Fig2 = new Rectangle(100, 50, 100, 50); //dimensions of Fig2
. . .
DrawFigure(e.Graphics, p, Fig1);
DrawFigure(e.Graphics, p, Fig2);
. . .
//remember to call FillFigure after drawing all figures.
FillFigure(e.Graphics, p, Fig1);
FillFigure(e.Graphics, p, Fig2);
. . .
}
private void DrawFigure(Graphics g, Pen p, Rectangle r)
{
g.DrawEllipse(p, r.X, r.Y, r.Width, r.Height);
}
private void FillFigure(Graphics g, Pen p, Rectangle r)
{
g.FillEllipse(new SolidBrush(this.BackColor), r.X + p.Width, r.Y + p.Width, r.Width - 2 * +p.Width, r.Height - 2 * +p.Width); //Adjusting Color so that it will leave border and fill
}

Don't have the time to solve it right now. But I'll give you what you need to work it out:
http://en.wikipedia.org/wiki/Triangle#The_sine.2C_cosine_and_tangent_rules
In the picture on wikipedia you see the triangle A,B,C. Let A be the center of the left circle, B the center of the right circle. And AC the radius of the left circle and BC the radius of the right circle.
Then point C would be the top intersection point. The corner in A, α, is half the angle in the left circle.The corner in b, β, half the angle in the right circle. These are the angles you need, right?
Wikipedia explains further: 'If the lengths of all three sides of any triangle are known the three angles can be calculated.'
Pseudocode:
a=radius_a
b=radius_b
c=b_x - a_x
alpha=arccos((b^2 + c^2 - a^2) / (2*b*c)) //from wikipedia
left_angle=2*alpha
Good luck :)

Related

Draw a triangle with 3 sides given

I want to draw a triangle with only the 3 sides length. (In C# Winforms)
Example: S1(3), S2(4), S3(5) with SN(x) as the length of each side.
I've tried to do this and the result is not a right triangle but it looks like it.
For the first side i just draw it like a line. and after i triy to find the angle with Pythagore and the same for the second one, but i realised that if i enter (5,4,3) it's something else.I'm just try to understand how can i find coordinates of a triangle with only the length of the sides.
Point a = new Point(0, 0);
Point b = new Point(s1, 0);
double y = (Math.Pow(s1, 2) + Math.Pow(s3, 2) - Math.Pow(s2, 2)) / (2 * s1);
double x = Math.Sqrt(Math.Pow(s3, 2) - Math.Pow(y, 2));
Point c = new Point((int)x, (int)y);
e.Graphics.DrawLine(Pens.Black, a, b);
e.Graphics.DrawLine(Pens.Black, b, c);
e.Graphics.DrawLine(Pens.Black, c, a);
That's the result:
Can someone help me? because I think I don't understand how can I do this.
This is more a math problem. At point A you have the sides s1, s3 with opposing side s2. The cosine formula then gives
2*s1*s3*cos(alpha) = s1^2+s3^2-s2^2.
Now the cosine is the projection of the angle to the horizontal axis, so you should have
x = s3*cos(alpha) = (s1^2+s3^2-s2^2)/(2*s1)
and correspondingly
y = sqrt(s3^2-x^2).
For the test side lengths 3,4,5 this would give
x = (3^2 + (5^2-4^2))/(2*3) = 3
y = sqrt(5^2-3^2) = 4
producing the points for the rectangular triangle.

find area of 2 coordinates

How can I find the area of the rectangle formed with 2 2-dimensional Vectors. ex:
p1 = (20, 40);
p2 = (30, 60);
I need to find the rectangle this forms. Is there a common used formula?
Just multiply the lengths of the sides of the rectangle, which are the differences between the coordinates of the two vectors:
(p2.X - p1.X) * (p2.Y - p1.Y)

Rotation of vector graphics

I have an application for drawing and editing vector graphics in WinForms
I have images, rectangles, ellipses, regions etc. and I know how to resize them by mouse move. But I don't know how to rotate them by mouse move.
I draw objects into Graphics.
I've tried this, but it didn't work.
g.TranslateTransform((float)(this.Rectangle.X + this.Rectangle.Width / 2), (float)(this.Rectangle.Y + this.Rectangle.Height / 2));
g.RotateTransform(this.Rotation);
g.TranslateTransform(-(float)(this.Rectangle.X + this.Rectangle.Width / 2), -(float)(this.Rectangle.Y + this.Rectangle.Height / 2));
//g.TranslateTransform(-(float)(rect.X + rect.Width / 2), -(float)(rect.Y + rect.Height / 2));
g.DrawImage(img, rect);
g.ResetTransform();
This didn't work, because I don't know how to find corners of objects in new (rotated) position, so I'm not able to resize that...
You need to apply high school trigonometry. There are lots of articles if you google "graphics.drawimage rotation"
But to start with, you should NOT be transforming the Graphics object itself. You are just looking to get the new bounding box of your image. To do this:
Take the bounding box of the image centered on the origin. Remember this is defined as three points for the benefit of DrawImage(Image, Point[])
Point[] boundingBox = { new Point(-width /2, -height/2),
new Point(width/2, -height/2),
new Point(-width/2, height/2) };
Use trig to rotate it. Feed each point through the following function:
Point rotatePointAroundOrigin(Point point, float angleInDegrees) {
float angle = angleInDegrees * Math.PI/180; // get angle in radians
return new Point( point.X * Math.Cos(angle) - point.Y * Math.Sin(angle),
point.X * Math.Sin(angle) + point.Y * Math.Cos(angle));
}
Translate the boundind box to where it has to go. Add the width/2 and height/2 to each of its points, plus whatever extra amount you want.
Call DrawImage(image, boundingBox)

Updating four points to stay in a Rectangle shape when one is moved

I am trying to draw a rectangular object that allows the user to click on a corner-point to resize and also rotate the rectangle in a 2D space.
Therefore I am using an array of four points ordered A, B, C, D (or 0, 1, 2, 3) from top-left to bottom-left in clockwise order.
The rotation works fine, I calculate the center point and rotate each point around it by an angle.
The resizing is done by determining which point was pressed down, and then setting its new position to the position of the mouse on each MouseMove event. The two adjacent points then need to be updated to stay in a rectangular shape. The resizing is intermittently not working. I have tried many ways to error-check, but all leave me with the same problem where if I move the mouse back and forth over the opposing point while moving a point, the points get distorted and are no longer a rectangular shape.
SOURCE CODE HERE
https://www.assembla.com/code/moozhe-testing/subversion/nodes/rotateRectangle
EXERPT OF PROBLEM CODE
private void MovePoint(int id, PointF newPoint)
{
PointF oldPoint = points[id];
PointF delta = newPoint.Substract(oldPoint);
PointF pointPrevious = points[(id + 3) % 4];
PointF pointNext = points[(id + 1) % 4];
PointF sidePrevious = pointPrevious.Substract(oldPoint);
PointF sideNext = pointNext.Substract(oldPoint);
PointF previousProjection = Projection(delta, sidePrevious);
PointF nextProjection = Projection(delta, sideNext);
pointNext = pointNext.AddPoints(previousProjection);
pointPrevious = pointPrevious.AddPoints(nextProjection);
points[(id + 3) % 4] = pointPrevious;
points[(id + 1) % 4] = pointNext;
points[id] = newPoint;
}
private PointF Projection(PointF vectorA, PointF vectorB)
{
PointF vectorBUnit = new PointF(vectorB.X, vectorB.Y);
vectorBUnit = vectorBUnit.Normalize();
float dotProduct = vectorA.X * vectorBUnit.X + vectorA.Y * vectorBUnit.Y;
return vectorBUnit.MultiplyByDecimal(dotProduct);
}
It sounds like you might want to be using a transformation matrix, instead of updating X/Y coordinates manually. Please check out this link:
Comparing GDI mapping modes with GDI+ transforms
Here's the MSDN reference:
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.transform

how to find the point exists in the area of any polygon or not?

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.

Categories