Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have drawn a rectangle. I know its (x1,y1) Top Left and (x2,y2) Bottom Right coordinates.. I also have the height h and width w of drawn rectangle.. How can I find the center coordinates (x,y) ?
I am currently using the following formula.
(x,y) = (x2 + x1)/2, (y2+y1)/2
It gives the correct y coordinate but no luck in x.
The center of rectangle is the midpoint of the diagonal end points of rectangle.
Here the midpoint is ( (x1 + x2) / 2, (y1 + y2) / 2 ).
That means:
xCenter = (x1 + x2) / 2
yCenter = (y1 + y2) / 2
Let me know your code.
Center x = x + 1/2 of width
Center y = y + 1/2 of height
If you know the width and height already then you only need one set of coordinates.
We can calculate using mid point of line formula,
centre (x,y) = new Point((boundRect.tl().x+boundRect.br().x)/2,(boundRect.tl().y+boundRect.br().y)/2)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
So I have a line, connecting two points, and I want to give this line a visual name in my canvas. I want this name to be positioned right at the middle of the line but with an offset so that the text doesn't go through the line. Determining the middle of the line isn't that hard you can just find it out like that:
And the line equation of the normal to the line can be determined pretty easy too:
Where you can find out n just by inserting P into the equation.
Now to the problem: For a given distance to the line, I want to find a point P(xn, yn) that is on that normal to the line through the middle of the line. After inserting the linear function into the distance formula I end up with this:
Not only am I not able to transform this equation to get a valid value for xn, I believe there has to be an mathematically easier way to achieve this.
Line middle is
mx = (x1 + x2)/2
my = (y1 + y2)/2
Line length is
len = sqrt((x1 - x2)^2 + (y1 - y2)^2)
Normalized direction vector of the line is
dx = (x2-x1) / len
dy = (y2-y1) / len
Perpendicular vector is
nx = -dy
ny = dx
Point at offset d from middle is
px = mx + d * nx
py = mx + d * ny
or for other side
px = mx - d * nx
py = mx - d * ny
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I know the centre of an ellipse, x0 and y0. I know its width and height.
How do I get the radius at any given angle from x0/y0?
Its conventional to describe an ellipse, centred at x0,y0, whose axes of symmetry are aligned with the coordinate axes by the equation
(x-x0)*(x-x0)/(a*a) + (y-y0)*(y-y0)/(b*b) = 1
Here a and b are constants that define the size and shape of the ellipse.
To get the width we put y=y0, and then the x values are
x = x0 + a
x = x0 - a
so the width is
w = 2*a
Similarly the height is
h = 2*b
If we have a point x,y on the ellipse and the angle between the x-axis and the vector x0,y0 -> x,y is theta then x,y can be written
x = x0 + r*cos(theta)
y = x0 + r*sin(theta)
We need to find r so that x,y is on the ellipse. Plugging these values into the equation for the ellipse and simplifying:
r = 1.0/hypot( cos(theta)/a, sin(theta)/b)
I'm guessing that by radius you mean the distance between x,y and x0,y0. This is r above
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Update
I do not think this question is off topic.
The solution provided is what I was looking for and it is a programming solution.
================
I want to know how can I find the coordinates of equal chords from the same point on the circle.
As shown in the image below, I will like to choose a random point on a circle and a random chord angle (in the example its 110 degrees).
I will know the radius (r) of the circle and one randomly selected point (A) on a circle.
Based on this data, I would like to know how can I draw two equal chords from this point (AB and AC) where AB = AC.
Let you have circle center xc, yc, radius R.
At first choose random angle in range 0..2*Pi
aangle = random(2*Pi)
Then A coordinates are
ax = xc + R * Cos(aangle)
ay = yc + R * Sin(aangle)
Now choose random (or you need specific value?) chord angle in needed range and get B, C coordinates
changle = random(3 * Pi / 4)
bx = xc + R * Cos(aangle + changle)
cx = xc + R * Cos(aangle - changle) // note subtraction
and similar for Y-coordinates
If you have A coordinates, you can also rotate them around center
bx = xc + (ax - xc) * Cos(changle) - (ay - yc) * Sin(changle)
and so on
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Suppose I have a line segment going from (x1,y1) to (x2,y2). How do I calculate the normal vector perpendicular to the line?
I can find lots of stuff about doing this for planes in 3D, but no 2D stuff.
Please go easy on the maths (links to worked examples, diagrams or algorithms are welcome), I'm a programmer more than I'm a mathematician ;)
If we define dx = x2 - x1 and dy = y2 - y1, then the normals are (-dy, dx) and (dy, -dx).
Note that no division is required, and so you're not risking dividing by zero.
Another way to think of it is to calculate the unit vector for a given direction and then apply a 90 degree counterclockwise rotation to get the normal vector.
The matrix representation of the general 2D transformation looks like this:
x' = x cos(t) - y sin(t)
y' = x sin(t) + y cos(t)
where (x,y) are the components of the original vector and (x', y') are the transformed components.
If t = 90 degrees, then cos(90) = 0 and sin(90) = 1. Substituting and multiplying it out gives:
x' = -y
y' = +x
Same result as given earlier, but with a little more explanation as to where it comes from.
We know that: if two vectors are perpendicular, their dot product equals zero.
The normal vector (x',y') is perpendicular to the line connecting (x1,y1) and (x2,y2). This line has direction (x2-x1,y2-y1), or (dx,dy).
So,
(x',y').(dx,dy) = 0
x'.dx + y'.dy = 0
The are plenty of pairs (x',y') that satisfy the above equation. But the best pair that ALWAYS satisfies is either (dy,-dx) or (-dy,dx)
m1 = (y2 - y1) / (x2 - x1)
if perpendicular two lines:
m1*m2 = -1
then
m2 = -1 / m1 //if (m1 == 0, then your line should have an equation like x = b)
y = m2*x + b //b is offset of new perpendicular line..
b is something if you want to pass it from a point you defined
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Maths101 question - does anyone know how to calculate an ellipse (width/height) that will enclose a given rectangle. Obviously there is no single ellipse - I'm after an algorithm that will give me various width/height combinations - or perhaps the smallest area of ellipse? It's for a GUI, so an aesthetically pleasing ratio of height/width is what I'm looking for.
Thanks in advance.
If you give your ellipse the same aspect ratio as the rectangle, you can work on the basis that what you want is a circle enclosing a square then stretched as if you've transformed the square into the required rectangle.
For a square with half side length = 1, the radius of the circle would be sqrt(2).
So, sweeping theta from 0 - 360', the ellipse's coordinate points will be:
x = cos(theta) * sqrt(2) * rect.width + x.center;
y = sin(theta) * sqrt(2) * rect.height + y.center;
where rect.width and rect.height are the half widths of the relevant sides.
Ellipse formula is (x/A)^2+(y/B)^2=1, where A and B are radiuses of ellipse
Rectangle sides are Rw and Rh
Let's assume we want ellipse with same proportions as rectangle; then, if we image square in circle (A=B,Rq=Rh) and squeeze it, we well keep ratio of ellipse A/B same as ratio of rectangle sides Rw/Rh;
This leads us to following system of equations:
(x/A)^2+(y/B)^2=1
A/B=Rw/Rh
Lets solve it:
A=B*(Rw/Rh)
(Rh/2B)^2+(Rh/2B)^2=1
Rh=sqrt(2)*B
And final solution:
A=Rw/sqrt(2)
B=Rh/sqrt(2)
Example:
The equation for a ellipse centered in the origin is
(x/A)^2 + (y/B)^2 = 1
Now if you want to enclose a rectangle of MxN with a eclipse you can move its center to the origin of coordinates. The top right coordinates are (M/2,N/2), replacing in the ellipse equation you have a formula you can use to solve B given A (or A given B).
If you have a rectangle of 4x2, the top-right coordinates are (2,1), replacing you have the (2/A)^2 + (1/B)^2 = 1, then if A=4 solving for B gives B=1/sqrt(1-(1/2)^2).
Experimentally, I found that an ellipse defined by a rectangle that is sqrt(2) larger than the inner rectangle works. So pass sqrt(2) to this function, and you will get the appropriate rectangle:
RectangleF boundingEllipse = GetScaledRectangle(innerRect, Convert.ToSingle(Math.Sqrt(2d)));
private RectangleF GetScaledRectangle(RectangleF rect, float scale)
{
float width = rect.Width * scale;
float height = rect.Height * scale;
float gap = width - rect.Width;
float left = rect.Left - (gap / 2f);
gap = height - rect.Height;
float top = rect.Top - (gap / 2f);
return new RectangleF(left, top, width, height);
}
Assuming you mean circumscribed (which is more precise than "enclosed"), you can read about how to circumscribe a rectangle here. From there, you can stretch it to rectangular, as Alnitak says.