Transforming a rectangle into a ring - math

I have a rectangle that I need to 'bend' into a ring, i.e. the top edge of the rectangle must map to the outer circle of the ring, the bottom to the inner circle, and the sides of the rectangle should join.
Here's an extremely crude sketch of the rectangle and ring:
If it is helpful or necessary, I can deal with the rectangle as a collection of horizontal lines, and the ring as a collection of circles.
The rectangle has a horizontal gradient from a to b that should map so that the gradient progresses on a circular direction.
I can see that this is a non-linear transform, but am lost as to where to look to learn the techniques to solve this problem. Could anyone with suitable experience in CG point me to anything like the right text, the right name of algorithm or the right graphics library to make my ring?

Try just using polar coordinates. If you map x as r and y as θ (normalising as θ runs from 0 to 2π), then adding some offset to r will vary the radius of the ring and adding an offset to θ will rotate it around the circle.
r = fx + a
g = (max_y - min_y)/(2*pi)
theta = gy + b
where a and b are these offsets, f scales the width of the ring and g normalizes the length of the rectangle to 2π. The transform back from these polar coordinates to cartesian (i.e. the screen) is just:
x' = r cos(theta)
y' = r sin(theta)
You then have 3 coordinate systems: (x,y) for the original rectangle, (r,θ) for the polar coordinates of the ring and (x',y') for the screen coordinates.

Related

How can I obtain UV coordinates of rectangle in 3D with raytracing method?

I am currently working on raytracing. I have problem with view Ray collisions. I cant figure out how to get intersection point of ray and plane, to be more precise, my problem is not figure out intersection point of ray vs plane, problem is to convert this coordinate into uv coordinate(this rectangle can be rotated anyhow in world) for texture mapping. I know One point on this rectangle, its normal and bounds.
We have 4 vertices of a rectangle lying on a sphere:
A - top left
B - top right
C - bottom right
D - bottom left
Center of the sphere:
O
And intersection point on the sphere inside rectangle ABCD:
I
The idea is to identify all sides of the triangle AID, because it will allow us to know the coordinates of the point I on the plane. So if we move the rectangle on the plane with A(0, rect.height) and D(0, 0) then point I could be found by solving the following system of equations:
x^2+y^2=DI^2 - circle equation with center in point D and radius DI
x^2+(y-rect.height)^2=AI^2 - circle equation with center in point A and radius AI
from which it follows that:
y = (DI^2-AI^2+rect.height) / (2*rect.height)
and x could have 2 values (positive and negative), however we are interested only in positive value, because only it will be inside the rect.
x = sqrt(DI^2-(DI^2-AI^2+rect.height)/(2*rect.height))
Then UV could be calculated the following way uv(x/rect.width, y/rect.height)
However length of AI and DI still not known, but could be calculated using formula of Great-circle distance
AI = (Radius of the Sphere) * (Angular orthodromy length must be in radians)
Radius of the Sphere = sqrt((O.x - A.x)^2+(O.y - A.y)^2+(O.z - A.z)^2)
Angular orthodromy length = arccos(sin(a1)*sin(a2)+cos(a1)*cos(a2)*cos(b2-b1))
a1 is angle AOA1, where A1(A.x, O.y, A.z)
b1 is angle O1OA1, where O1(O.x, O.y, A.z)
a2 is angle IOI1, where I1(I1.x, O.y, I.z)
b2 is angle O2OI1, where O2(O.x, O.y, I.z)

Rectangle rotation around clipping rectangle center

I have two rectangles where one is a clipping for the other one.
Now I want to rotate the bigger rectangle around the center of the clipping rectangle and adjust x/y values.
How can I calculate the new x/y values after rotation?
I actually just want to rotate the x/y of the bigger box around the center of the smaller box. So the x/y point of the bigger box is relative to the top/left point of the smaller box. I have the width and height of the smaller box so I can calculate x/y point of the big box relative to the center of small box. The angle to rotate is in degrees. The rotation can be any degree, for example 10.
You can do as follows:
determine the angle by which you want to rotate, make sure it suitable for the trigonometric functions (sin(), cos(), ...), i.e. right angle is usually Pi/2
in case of rotating counterclockwise, it is negative
determine the coordinates of c, as cx,cy
process each of the corners of the rectanlge, one by one, for a total of four
for each corner P, currently at coordinates px,py and to move to px2,py2
determine angle between current P and C, using atan2(py-cy, px-cx)
to get from degrees to radians (for use with trigonometry) calculate radians=(pi*degrees)/180.0
add the desired rotation angle to that current angle, to get newangle
determine the distance of current P to C, sqrt((px-cx)(px-cx) + (py-cy)(py-cy))
multiply the distance (which is not changing by rotation), with the appropriate trigonometric function
px2 = distance * cos(newangle)
py2 = distance * sin(newangle)
If you want to rotate a given point P around a point C, which are defined in the same coordinate system you can use a simple rotation matrix. Calculate the P coordinates with respect to C (subtraction), then apply rotation with the matrix and go back to original coordinates by adding C again.
All that matters is the coordinates of the rotation center and the angle.
The most compact formulation is by means of complex numbers (of which I hope you have some understanding; you actually don't need a complex data type, you can expand the formulas).
Let C be the center and α the angle. Then for any point P, the image Q is given by
Q = (P - C) cis(α) + C
where cis(α) = cos(α) + i sin(α).
The inverse rotation is simply given by
P = (Q - C) cis(-α) + C.

how to get cordinates of points drawn on ractangle perimeter using circular function?

I have rectangle with known base and height. I need coordinates of points on rectangle perimeter which is obtained by drawing lines from the center of base after every known degree (Say 1 degree) for the range of 0 -180 degree.
To get my question clearly i have attached image below. Kindly have a look.
Clues for a possible approach:
Find x-coordinate of intersection of ray with angle Theta and top edge of rectangle (note special cases Theta = 0, Theta = Pi)
If this coordinate lies outside of rectangle, find y-coordinate of intersection of ray and vertical edge (choose right one depending on angle)

Plot 2d point on 3D plane drawn in 2d

I am attempting to draw a point from a 2d plane on a 3d plane that is drawn in 2d. I'm not sure how to adjust the y position based on the angle of the perspective. As you can see in the image linked below (Stack Overflow won't let me include the image because I just signed up), if the point is at the center point of the rectangle, it would need to be shifted up slightly when viewed from an angle to account for distance from the viewer. Can anyone provide an equation to help?
Say the point in the rectangle is given by (x,y), and the coordinates we're looking for in the second image are (x', y').
w = y + y0
y' = k atan(w/h)
r = sqrt(h2 + w2)
x' = k atan(x/r)
where k is a scaling factor for the whole image, h is "altitude of the viewpoint above the plane" and y0 is, roughly, distance to the object.

Translation coordinates for a circle under a certain angle

I have 2 circles that collide in a certain collision point and under a certain collision angle which I calculate using this formula :
C1(x1,y1) C2(x2,y2)
and the angle between the line uniting their centre and the x axis is
X = arctg (|y2 - y1| / |x2 - x1|)
and what I want is to translate the circle on top under the same angle that collided with the other circle. I mean with the angle X and I don't know what translation coordinates should I give for a proper and a straight translation!
For what I think you mean, here's how to do it cleanly.
Think in vectors.
Suppose the centre of the bottom circle has coordinates (x1,y1), and the centre of the top circle has coordinates (x2,y2). Then define two vectors
support = (x1,y1)
direction = (x2,y2) - (x1,y1)
now, the line between the two centres is fully described by the parametric representation
line = support + k*direction
with k any value in (-inf,+inf). At the initial time, substituting k=1 in the equation above indeed give the coordinates of the top circle. On some later time t, the value of k will have increased, and substituting that new value of k in the equation will give the new coordinates of the centre of the top circle.
How much k increases at value t is equal to the speed of the circle, and I leave that entirely up to you :)
Doing it this way, you never need to mess around with any angles and/or coordinate transformations etc. It even works in 3D (provided you add in z-coordinates everywhere).

Resources