How do you calculate the x,y points of all corners of a square from two only a pair of x,y points (see pic) - trigonometry

I have the green x,y points, how would I get the missing red?

You can rotate the two known points of 90° around their midpoint.
In pseudo code:
// Evaluate the midpoint from the coordinates of points a and b,
h_x = (b_x - a_x) / 2;
h_y = (b_y - a_y) / 2;
m_x = a_x + h_x;
m_y = a_y + h_y;
// Apply a rotation of 90 degree around the midpoint to find c and d
c_x = m_x - h_y;
c_y = m_y + h_x;
d_x = m_x + h_y;
d_y = m_y - h_x;
This result can be formally derived in terms of homogeneous coordinates and transfomation matrices.
The midpoint m, expressed in homogeneous coordinates, can be calculated as
To rotate a vector around the origin of an angle α, we apply a rotation matrix like
If another center of rotation is needed (the midpoint, in our case), we need to translate from the original position to the origin, apply the rotation and translate back again. The translation matrices are
The complete transformation can be expressed as
Where
So that we can evaluate, let's say d, with
Q.e.d.

Related

How to find the orientation of a plane?

I have three non-colinear 3D points, let's say pt1, pt2, pt3. I've computed the plane P using the sympy.Plane. How can I find the orientation of this plane(P) i.e. RPY(euler angles) or in quaternion?
I never used sympy, but you should be able to find a function to get the angle between 2 vectors (your normal vector and the world Y axis.)
theta = yaxis.angle_between(P.normal_vector)
then get the rotation axis, which is the normalized cross product of those same vectors.
axis = yaxis.cross(P.normal_vector).normal()
Then construct a quaternion from the axis and angle
q = Quaternion.from_axis_angle(axis, theta)

extract mean of circle patche in python

i have some X/Y coordinates that represent the center of a circle somewhere in an image. From that circle, i want to compoute the mean of all the point contained inside the circle.
currently, i compute the mean of a square patche as follow, but a square is not relevent for the project. Ideally, i would like to do it only with numpy. but if it is not possible, i would concidere something else.
mean = np.mean(image[Y - margin : Y + margin, X - margin, X + margin])
As I understood,
YOU HAVE: (x,y) of the center of the circle
YOU WANT: mean of all the points contained in the circle
Since all the points on the right side should be equal to the number of points on the left side of the center, Wouldn't the mean be the same as the center of the circle !?
i found a solution where I compute all indexes contained in a centered disk.
I calculate the squarred euclidean distance of each X/Y coordinate of a squarre array.
I compare it to the squarred radius of the circle.
If it is superior, the point is not contained in the circle. Mark it as 0, 1 otherwise
I extract indexes where the array is equal to one (disk-shaped)
I center the computed indexes. To use them, i add the X/Y coordinate of a specific image point.
NOTE: I used the squarred euclidean distance because the square root function is monotonic (i.e constantly increasing). So, it saves compational power to keep with the squarred version.
radius = 4
size = 2 * radius + 1
radiusSquarred= radius**2
mask = np.zeros((size, size))
distance = lambda x, y: (x-radius)**2 + (y-radius)**2
for i in range(size):
for j in range (2 * radius+ 1):
if distance(i, j) <= radiusSquarred:
mask[i, j] = 1
index = np.where(mask == 1)
diskIndexes = (index[0] - radius, index[1] - radius)
X, Y = 100, 150
np.mean(image[diskIndexes[0] + Y, diskIndexes[1] + X])

Vertex of a 3D triangle that is closest to a point given barycentric parameter of intersection

So I have this problem: I'm writing a program where you can click on a point on the screen with a 3D object projected and the vertex of triangle mesh that is visually (in 2D) closest to that point will be selected. I have calculated the barycenter coordinates of the intersection with a triangle. How can I use those barycentric coordinates to calculate the closest vertex?
Thanks for help
For barycentric coordinates (u,v,w) and triangle side length a,b,c squared distance to A, B, C vertices are
dA^2 = -(a^2*v*w + b^2*w*(u-1) + c^2*v*(u-1)) =
-(a^2*v*w+b^2*w*u+c^2*v*u - b^2*w - c^2*v) =
b^2*w + c^2*v - Const (TheSameConstantForAllVertices)
dB^2 = -(a^2*(v-1)*w + b^2*w*u + c^2*(v-1)*u) =
a^2*w + c^2*u - Const
dC^2 = -(a^2*v*(w-1) + b^2*(w-1)*u + c^2*v*u)
a^2*v + b^2*u - Const
(more formulas here), so you can compare squared distances and choose the smallest one. I've separated simpler expression for comparison.
Perhaps you need some approximation for rather good (close to equilateral) triangles. In this case choose the largest value from barycentric triplet. It roughly corresponds to the closest vertex.

Find a coordinate along a path

My trigonometry needs a little help.
How would I go about calculating the point of the nearest possible intersection with a line along a rounded corner?
Take this image:
What I would like to know is, given that I know point a, and the dimensions of the rectangle, how would I find point b when the edges of the rectangle are curved?
So far, as you can see, I've only managed to calculate the nearest edge of the rectangle as if it had right-angled corners.
If it matters, I'm doing this in ActionScript 3. But example sudo-code will suffice.
Calculate the vector from the midpoint M of the corner to A:
v_x = a_x - m_x
v_y = a_y - m_y
then go radius of the corner r times towards A to get to the intersection point I
i_x = m_x + r*v_x
i_y = m_y + r*v_y
This obviously only works if the nearest intersection is on the rounded corner. Just calculate the other intersections with the edges, too, and then check which has the nearest distance to A.
You need to know the radius R of the circle that generates the round corner and the coordinates (Xr,Yr) of the point where the two sides of a non rounded rectangle cross each other.
Then the coordinates for the center of the circle that generates the round corner are (Xc, Yc) = (Xr-R, Yr-R)
From here, it's a matter of solving the equation of the cross point between the segment line defined by point A=(Xa, Ya) and point (Xc, Yc), whose parametric equation is:
x = Xa + p*(Xc-Xa)
y = Ya + p*(Yc-Ya)
and the circle whose equation is
(x-Xc)^2 + (y-Yc)^2 = R^2
Substitute values for x and y from the parametric euation of the line in the equation of the circle, and you will have an equation with only one unkown: p. Solve the equation, and if there are more than one solution, choose the one that is in the range [0,1]. Substitute the found value of p in the parametric equation of the line to get the point of intersection.
Graphically:
If you know the radius and center of the corner as R and C=(Xc, Yc), then the nearest point on the corner to the given point A=(Xa, Ya) is the intersection point of the corner and the line defined by the given point and the center. This point can be directly expressed as
X = Xc + R*(Xa-Xc)/|AC|
Y = Yc + R*(Ya-Yc)/|AC|
where |AC| = Sqrt((Xa-Xc)^2 + (Ya-Yc)^2)

How to determine whether a point (X,Y) is contained within an arc section of a circle (i.e. a Pie slice)?

Imagine a circle. Imagine a pie. Imagine trying to return a bool that determines whether the provided parameters of X, Y are contained within one of those pie pieces.
What I know about the arc:
I have the CenterX, CenterY, Radius, StartingAngle, EndingAngle, StartingPoint (point on circumference), EndingPoint (point on circumference).
Given a coordinate of X,Y, I'd like to determine if this coordinate is contained anywhere within the pie slide.
Check:
The angle from the centerX,centerY through X,Y should be between start&endangle.
The distance from centerX,centerY to X,Y should be less then the Radius
And you'll have your answer.
I know this question is old but none of the answers consider the placement of the arc on the circle.
This algorithm considers that all angles are between 0 and 360, and the arcs are drawn in positive mathematical direction (counter-clockwise)
First you can transform to polar coordinates: radius (R) and angle (A). Note: use Atan2 function if available. wiki
R = sqrt ((X - CenterX)^2 + (Y - CenterY)^2)
A = atan2 (Y - CenterY, X - CenterX)
Now if R < Radius the point is inside the circle.
To check if the angle is between StartingAngle (S) and EndingAngle (E) you need to consider two possibilities:
1) if S < E then if S < A < E the point lies inside the slice
2) if S > E then there are 2 possible scenarios
if A > S
then the point lies inside the slice
if A < E
then the point lies inside the slice
In all other cases the point lies outside the slice.
Convert X,Y to polar coordinates using this:
Angle = arctan(y/x);
Radius = sqrt(x * x + y * y);
Then Angle must be between StartingAngle and EndingAngle, and Radius between 0 and your Radius.
You have to convert atan2() to into 0-360 before making comparisons with starting and ending angles.
(A > 0 ? A : (2PI + A)) * 360 / (2PI)

Resources