We know 2 circle's x and y center position, and the radius is the same. I want to visually connect the circles without looping the draw ellipse for each point on the line what connects the 2 circle's center.
From this:
To this:
Code:
int radius = 75;
int x1 = 100;
int y1 = 200;
int x2 = 300;
int y2 = 100;
g.FillEllipse(Brushes.Blue, new Rectangle(x1 - radius / 2, y1 - radius / 2, radius, radius));
g.FillEllipse(Brushes.Blue, new Rectangle(x2 - radius / 2, y2 - radius / 2, radius, radius));
A solution for when the Circles don't have the same Diameter.
The first information needed is the distance between the Centers of two Circles.
To calculate it, we use the Euclidean distance applied to a Cartesian plane:
Where (x1, y1) and (x2, y2) are the coordinates of the Centers of two Circles.
We also need to know the Direction (expressed as a positive or negative value): the calculated [Distance] will always be positive.
in C# it, it can be coded as:
float Direction = (Circle1Center.X > Circle2Center.X) ? -1 : 1;
float Distance = (float)Math.Sqrt(Math.Pow(Circle1Center.X - Circle2Center.X, 2) +
Math.Pow(Circle1Center.Y - Circle2Center.Y, 2));
Distance *= Direction;
Now, we have the Distance between the Centers of two Circles, which also expresses a direction.
We also need to know how this virtual line - connecting the two Centers - is rotated in relation to our drawing plane. In the figure below, the Distance can be viewed as the hypotenuse of a right triangle h = (A, B). The C angle is determined by the intersection of the straight lines, parallel to the axis, that cross the Centers of the Circles.
We need to calculate the angle Theta (θ).
Using the Pythagorean theorem, we can derive that the Sine of the angle Theta is Sinθ = b/h (as in the figure)
Using the Circles' Centers coordinates, this can be coded in C# as:
(Distance is the triangle's hypotenuse)
float SinTheta = (Math.Max(Circle1Center.Y, Circle2Center.Y) -
Math.Min(Circle1Center.Y, Circle2Center.Y)) / Distance;
SinTheta expresses an angle in Radians. We need the angle expressed in Degrees: the Graphics object uses this measure for its world transformation functions.
float RotationAngle = (float)(Math.Asin(SinTheta) * (180 / Math.PI));
Now, we need to build a Connector, a shape that links the 2 Circles. We need a Polygon; a Rectangle can't have different pairs of sides (we are considering Circles with different Diameters).
This Polygon will have the longer sides = to the Distance between the Circles Centers, the shorter sides = to the Circles Diameters.
To build a Polygon, we can use both Graphics.DrawPolygon and GraphicsPath.AddPolygon. I'm choosing the GraphicsPath method, because a GraphicsPath can hold more that one shape and these shapes can interact, in a way.
To connect the 2 considered Circles with a Polygon, we need to rotate the Polygon using the RotationAngle previously calculated.
A simple way to perform the rotation, is to move the world coordinates to the Center of one of the Circles, using the Graphics.TranslateTransform method, then rotate the new coordinates, using Graphics.RotateTransform.
We need to draw our Polygon positioning one of the short sides - corresponding to the Diameter of the Circle which is the center of the coordinates transformation - in the center of the Cirle. Hence, when the rotation will be applied, it's short side it will be in the middle of this transformation, anchored to the Center.
Here, figure 3 shows the positioning of the Polygon (yellow shape) (ok, it looks like a rectangle, never mind);in figure 4 the same Polygon after the rotation.
Notes:
As TaW pointed out, this drawing needs to be performed using a SolidBrush with a non-transparent Color, which is kind of disappointing.
Well, a semi-transparent Brush is not forbidden, but the overlapping shapes will have a different color, the sum of the transparent colors of the intersections.
It is however possible to draw the shapes using a semi-transparent Brush without a Color change, using the GraphicsPath ability to fill its shapes using a color that is applied to all the overlapping parts. We just need to change the default FillMode (see the example in the Docs), setting it to FillMode.Winding.
Sample code:
In this example, two couples of Circles are drawn on a Graphics context. They are then connected with a Polygon shape, created using GraphicsPath.AddPolygon().
(Of course, we need to use the Paint event of a drawable Control, a Form here)
The overloaded helper function accepts both the Circles' centers position, expressed as a PointF and a RectangleF structure, representing the Circles bounds.
This is the visual result, with full Colors and using a semi-transparent brush:
using System.Drawing;
using System.Drawing.Drawing2D;
private float Radius1 = 30f;
private float Radius2 = 50f;
private PointF Circle1Center = new PointF(220, 47);
private PointF Circle2Center = new PointF(72, 254);
private PointF Circle3Center = new PointF(52, 58);
private PointF Circle4Center = new PointF(217, 232);
private void form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
DrawLinkedCircles(Circle1Center, Circle2Center, Radius1, Radius2, Color.FromArgb(200, Color.YellowGreen), e.Graphics);
DrawLinkedCircles(Circle3Center, Circle4Center, Radius1, Radius2, Color.FromArgb(200, Color.SteelBlue), e.Graphics);
//OR, passing a RectangleF structure
//RectangleF Circle1 = new RectangleF(Circle1Center.X - Radius1, Circle1Center.Y - Radius1, Radius1 * 2, Radius1 * 2);
//RectangleF Circle2 = new RectangleF(Circle2Center.X - Radius2, Circle2Center.Y - Radius2, Radius2 * 2, Radius2 * 2);
//DrawLinkedCircles(Circle1, Circle2, Color.FromArgb(200, Color.YellowGreen), e.Graphics);
}
Helper function:
public void DrawLinkedCircles(RectangleF Circle1, RectangleF Circle2, Color FillColor, Graphics g)
{
PointF Circle1Center = new PointF(Circle1.X + (Circle1.Width / 2), Circle1.Y + (Circle1.Height / 2));
PointF Circle2Center = new PointF(Circle2.X + (Circle2.Width / 2), Circle2.Y + (Circle2.Height / 2));
DrawLinkedCircles(Circle1Center, Circle2Center, Circle1.Width / 2, Circle2.Width / 2, FillColor, g);
}
public void DrawLinkedCircles(PointF Circle1Center, PointF Circle2Center, float Circle1Radius, float Circle2Radius, Color FillColor, Graphics g)
{
float Direction = (Circle1Center.X > Circle2Center.X) ? -1 : 1;
float Distance = (float)Math.Sqrt(Math.Pow(Circle1Center.X - Circle2Center.X, 2) +
Math.Pow(Circle1Center.Y - Circle2Center.Y, 2));
Distance *= Direction;
float SinTheta = (Math.Max(Circle1Center.Y, Circle2Center.Y) -
Math.Min(Circle1Center.Y, Circle2Center.Y)) / Distance;
float RotationDirection = (Circle1Center.Y > Circle2Center.Y) ? -1 : 1;
float RotationAngle = (float)(Math.Asin(SinTheta) * (180 / Math.PI)) * RotationDirection;
using (GraphicsPath path = new GraphicsPath(FillMode.Winding))
{
path.AddEllipse(new RectangleF(-Circle1Radius, -Circle1Radius, 2 * Circle1Radius, 2 * Circle1Radius));
path.AddEllipse(new RectangleF(-Circle2Radius + (Math.Abs(Distance) * Direction),
-Circle2Radius, 2 * Circle2Radius, 2 * Circle2Radius));
path.AddPolygon(new[] {
new PointF(0, -Circle1Radius),
new PointF(0, Circle1Radius),
new PointF(Distance, Circle2Radius),
new PointF(Distance, -Circle2Radius),
});
path.AddEllipse(new RectangleF(-Circle1Radius, -Circle1Radius, 2 * Circle1Radius, 2 * Circle1Radius));
path.AddEllipse(new RectangleF(-Circle2Radius + (Math.Abs(Distance) * Direction),
-Circle2Radius, 2 * Circle2Radius, 2 * Circle2Radius));
path.CloseAllFigures();
g.TranslateTransform(Circle1Center.X, Circle1Center.Y);
g.RotateTransform(RotationAngle);
using (SolidBrush FillBrush = new SolidBrush(FillColor)) {
g.FillPath(FillBrush, path);
}
g.ResetTransform();
}
}
As the other answers so far slightly miss the correct solution, here is one that connects two circles of equal size:
using (Pen pen = new Pen(Color.Blue, radius)
{ EndCap = LineCap.Round, StartCap = LineCap.Round } )
g.DrawLine(pen, x1, y1, x2, y2);
Notes:
Usually is is good idea to set the smoothing mode of the graphics object to anti-alias..
To connect two circles of different sizes will take some math to calculate the four outer tangent points. From these one can get a polygon to fill or, if necessary one could create a GraphicsPath to fill, in case the color has an alpha < 1.
Jimi's comments point to a different solution that make use of GDI+ transformation capabilities.
Some of the answers or comments refer to the desired shape as an oval. While this ok in common speech, here, especially when geometry books are mentioned, this is wrong, as an oval will not have any straight lines.
As Jimi noted, what you call radius is really the diameter of the circles. I left the wrong term in the code but you should not!
Pseudo style:
circle1x;
circle1y;
circle2x;
circle2y;
midx=circle1x-circle2x;
midy=circle2x-circle2x;
draw circle at midx midy;
repeat for midx midy, in both directions. add another circle. honestly man, this isnt worth it,in order to make it smooth, you will need several circles. you need to draw an oval using the center of both circles as the two centers of your oval
I'm working on a rather Large project. It was already finished when I started and I have to implement some small gimics.
One of those is the rotation of a marker on a map.
When the marker is selected a rectangle (System.Wndows.FrameWorkElement) is drawn around the picture. Since I would basically have to rewrite the whole program to use another rectangle, I have to stick with the framework element.
To rotate this thing, I added a line and a circle.
The line connects the circle with the rectangle. When the user clicks on the circle and drags the mouse, the whole thing is supposed to rotate around the center of the rectangle.
So far, the rotation of the rectangle and the line works fine. But the circle, though it is rotating around the center of the rectangle, is also rotating around a point at it's own border.
I rotate the rectangle with a RenderTransform object, which works well enough and is easy enough.
For the line and the circle, I wrote a method to calculate the rotation.
The line I can calculate without using the angle.
Here's the method:
private void SetPositionOfRotationShaft(Point center)
{
double l = Math.Sqrt(Math.Pow((this.ConnectionLineDirection.X - center.X), 2) + Math.Pow((this.ConnectionLineDirection.Y - center.Y), 2));
double factor = Math.PI / 180;
this.connectionLine.X1 = center.X + (this.surroundingRectangle.Height / (2 * l)) * (this.ConnectionLineDirection.X - center.X);
this.connectionLine.Y1 = center.Y + (this.surroundingRectangle.Height / (2 * l)) * (this.ConnectionLineDirection.Y - center.Y);
this.connectionLine.X2 = center.X + ((this.surroundingRectangle.Height + 40) / (2 * l)) * (this.ConnectionLineDirection.X - center.X);
this.connectionLine.Y2 = center.Y + ((this.surroundingRectangle.Height + 40) / (2 * l)) * (this.ConnectionLineDirection.Y - center.Y);
double translatedLeft = Canvas.GetLeft(this.rotationSign) - center.X;
double translatedTop = Canvas.GetTop(this.rotationSign) - center.Y;
double left = ((translatedLeft * Math.Cos(-this.rotateSurroundingRectangle.Angle*factor)) + (translatedTop * Math.Sin(-this.rotateSurroundingRectangle.Angle*factor))) + center.X;
double top = ((translatedTop * Math.Cos(-this.rotateSurroundingRectangle.Angle * factor)) - (translatedLeft * Math.Sin(-1 * this.rotateSurroundingRectangle.Angle * factor))) + center.Y;
Canvas.SetLeft(this.rotationSign, left);
Canvas.SetTop(this.rotationSign, top);
}
Also curious, when i use the same calculation for the line as i do for the circle, the line rotates at a higher speed. The same thing happend to the circle until i added the factor.
So, the problem was, that i had to set the position of the circle with Canvas.SetLeft() and SetTop(), which is essentialy the upper left corner of a square around the circle.
For my rotation to work, i should have set the center (but that's not possible). So i had to subtract the radius of the circle from top and left.
Canvas.SetLeft(this.rotationSign, left-radius);
Canvas.SetTop(this.rotationSign, top-radius);
I would like to draw a radar on a pictureBox. Drawing points is no problem but I am struggling with basic maths. Maybe I am too tired.
I have a pictureBox that is 200x200. I have loaded a small, centered image inside the picturebox (4x4) which symbolizes the current player.
I have build a function called
PaintRadar(int meX, int meY, int enemyX, int enemyY)
The parameters could have the following values: meX = 27000, meY = 30000, enemyX = 26000, enemyY = 28000
The desired result is to have the enemies around me and I am always centered in the pictureBox. What do I have to calculate to center meX and meY in the pictureBox?
Thanks
Assume the player is in the middle of the enemies and draw the enemies around the center based on the difference between their positions and the player's position.
Think about it as though the player is the origin. By subtracting the player's position from the enemy's position you are putting the enemy position into a coordinate system with the player at the center. This is essentially what you're radar is.
Example:
// Get differences. d is short for difference (or delta :)).
int dy = enemyY - meY;
int dx = enemyX - meX;
// Then scale the dy and dx values so they fix in the picture box.
dy *= scaleY;
dx *= scaleX;
Then you would draw the enemies at (dx,dy) on the picture box.
Scale should be a formula like this:
scaleY = (1 / maxDetectionDistance) * (heightOfRadarBox / 2);
scaleX = (1 / maxDetectionDistance) * (widthOfRadarBox / 2);
Anything greater than your radar's limit should not be drawn.
// Don't draw if enemy is too far away for radar to pick up.
if (Math.Abs(dy) > maxDetectionDistance || Math.Abs(dx) > maxDetectionDistance)
{
return;
}
I am drawing 10 circles in an array, these circles are moving around and bouncing across the screen. How would I take these drawn circles and detect when they collide with each other. When they collide I need them to bounce off of each other. These circles have random widths and heights. random speeds and all spawn at center screen.
How the circles are drawn:
private void pbGamescreen_Paint(object sender, PaintEventArgs e)
{
for (int mt = 0; mt < spawn; mt++)
{
e.Graphics.FillEllipse(ballBrush[mt], (int)xPos[mt], (int)yPos[mt], ballSizex[mt], ballSizey[mt]);
e.Graphics.DrawEllipse(Pens.Gray, (int)xPos[mt], (int)yPos[mt], ballSizex[mt], ballSizey[mt]);
}
}
Two circles intersect if the distance between their center points is smaller than the addition of their radiuses. You need to iterate each of your circle and check this against each other circle.
For instance, say you have these two circles on a horizontal axis:
(-----o-----) (---o---)
They do not intersect, as the distance between their center points is 12, and the sum of their radiuses is 8. However, these two do:
(-----o----(-)--o---)
The formula for the distance between two 2D points is:
var xdiff = x2 - x1;
var ydiff = y2 - y1;
return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
if r1 and r2 are radiuses of the two circles, and d is the distance between the centers of teh two circles then
bounce off when d<=r1 + r2;
ideally you should do it when d == r1 + r2;
Just a suggestion:
also keep the mass of the circle proportional to their r(radius) and then using the law of conservation momentum m1v1 = m2v2; bounce them offf in a way that looks real
I want to know the vertices of the arc where I have its StartPoint, Center and the EndPoint as well as the radius of the arc. I am aware that the arc which is drawn is by creating a small lines with the precision which is specified in the parameter. What I am trying to achieve is calculate the area of a polygon which may have an arc in it which can look like the image I have attached with this question.
SP = StartPoint of the Arc.
EP = EndPoint of the Arc.
CP = Center of the Arc.
Knowing StartPoint, Center and the EndPoint of arc is not enough to define it uniquely. You have to knew some another parameter(s).
When arc is well defined, it is possible to calculate an area of circular segment geometrically
Edit: Because you also know radius R, we can calculate an area:
Theta = 2 * ArcSin(Distance_SPtoEP/(2*R))
Area = 1/2 * R * R * (Theta - Sin(Theta))
Quick check:
R = 1, semicircle.
Theta = 2 * ArcSin(2/2) = 2 * Pi/2 = Pi
Area = 1/2 * (Pi - 0) = Pi/2 - true
Edit2: It is simpler to connect SP and EP to get remaining polygon, than build polyline approximation of arc and calc area of hundred-vertice polygon.
Polyline approximation:
We want that arc-line distance doesn't excess some limit d. So we will calculate small arc angle A
d = R * (1-Cos(A/2))
A = 2 * ArcCos(1-d/R)
Now divide large arc to small pieces with angle A, and generate new vertices
Your drawing seems to indicate a half ellipse.
The long axis (A) would be SP-EP and the short axis (B) should be given. The area is Pi.A.B/8.