I have two applications, one that creates a rectangle, and output's it's left,top,width,height and rotation angle, and another that draws the rectangle to the screen.
Application1 (writer) uses the top, left as the origin for rotation.
Application2 (reader) uses the center as the origin for rotation.
I'm trying to get a deltaX, and deltaY so that I can draw the rectangle, rotate it about the origina, and use a built in translate method, to shift the rectangle into position.
I know that a points rotation can be calculated as follows:
x' = x*cos(theta) - y*sin(theta)
y' = x+sin(theta) + y*cos(theta)
But, with that, I can't seem to figure out the actual delta values needed for the translate method.
In the image below, the white rectangle is drawn using top/left as the rotation point, while the green rectangle is drawn using center as the rotation point. I'd like to shift the green rectangle onto the white one.
(0,0)
You have to translate by:
Development:
In application 1, the top left vertex of a rectangle shall be .
Since application 1 rotates rectangles about the top left vertex, their rotated position coincides:
Application 2 uses center coordinates, which relate to top-left point in application 1 as follows:
To calculate the rotated top-left vertex in application 2, we apply the rotation matrix R:
So the translation vector T is (substituting equations above):
Related
I am making this game where a sprite of a circle( origin set at the center of the sprite) is being up-scaled and rotated simultaneously. What I need to determine is the change in the radius of the circle in relation to scaling.How do I go about doing that? What exactly does scaling do? I mean what does 2x scale mean? does it mean my sprite has twice the area than previous? Btw , I am using LibGDX.
I figured it out myself.The problem was that the bounding rectangle of a sprite changes in size as the sprite is being rotated.So the raidous of the circle can not be determined using the with of the rectangle at that time. What I found was that the width of the bounding rectangle is the product of the scale and the initial width .So I can calculate what the width would be at any instance from that even if the sprite is rotating. And I get the radius from that.
I have given an assignment of to project a object in 3D space into a 2D plane using simple graphics in C. The question is that a cube is placed in fixed 3D space and there is camera which is placed in a position whose co-ordinates are x,y,z and the camera is looking at the origin i.e. 0,0,0. Now we have to project the cube vertex into the camera plane.
I am proceeding with the following steps
Step 1: I find the equation of the plane aX+bY+cZ+d=0 which is perpendicular to the line drawn from the camera position to the origin.
Step 2: I find the projection of each vertex of the cube to the plane which is obtained in the above step.
Now I want to map those vertex position which i got by projection in step 2 in the plane aX+bY+cZ+d=0 into my screen plane.
thanks,
I don't think that by letting the z co-ordinate equals zero will lead me to the actual mapping. So any help to figure out this.
You can do that in two simple steps:
Translate the cube's coordinates to the camera's system (using
rotation), such that the camera's own coordinates in that system are x=y=z=0 and the cube's translated z's are > 0.
Project the translated cube's coordinates onto a 2d plain by dividing its x's and y's by their respective z's (you may need to apply a constant scaling factor here for the coordinates to be reasonable for the screen, e.g. not too small and within +/-half the screen's height in pixels). This will create the perspective effect. You can now draw pixels using these divided x's and y's on the screen assuming x=y=0 is the center of it.
This is pretty much how it is done in 3d games. If you use cube vertex coordinates, then you get projections of its sides onto the screen. You may then solid-fill the resultant 2d shapes or texture-map them. But for that you'll have to first figure out which sides are not obscured by others (unless, of course, you use a technique called z-buffering). You don't need that for a simple wire-frame demo, though, just draw straight lines between the projected vertices.
I'm working on a small webapp in which I need to rotate shapes. I
would like to achieve this by grabbing a point on a circle and
dragging it around to rotate the image.
Here's a quick illustration to help explain things:
My main circle can be dragged anywhere on the canvas. I know it's
radius (r) and where 12 o'clock (p0) will always be (cx, cy - r). What
I need to know is what degree p1 will be (0-360º) so I can rotate the
contents of the main circle accordingly with Raphael.rotate().
I've run through a bunch of different JavaScript formulations to find this (example), but none seem to give me values between 0-360 and my basic math skills
are woefully deficient.
The Color Picker demo (sliding the cursor along the ring on the right) has the behavior I want, but even after poring over the source code I can't seem to replicate it accurately.
Anything to point me in the correct direction would be appreciated.
// Angle between the center of the circle and p1,
// measured in degrees counter-clockwise from the positive X axis (horizontal)
( Math.atan2(p1.y-cy,p1.x-cx) * 180/Math.PI + 360 ) % 360
The angle between the center of the circle and p0 will always be +90°. See Math.atan2 for more details.
I've just started using the turtle graphics program, but I can't figure out how to move the turtle automatically to the center of a circle (no matter where the circle is located) without it drawing any lines. I thought I could use the goto.() function but it's too specific and I need something general.
Use penup to lift the pen and draw nothing while moving.
If your rotate 90 degrees left and then move forward one radius length you will be at the centre of the circle (and lifting the pen first to stop it drawing a line as outis said).
for example
import turtle
myT=turtle.Turtle()
# draw your circle
myT.circle(100)
# rotate so you are looking towards the centre of the circle
myT.left(90)
# lift the pen so no line is drawn
myT.penup()
myT.forward(100)
# put pen down now (if you need to)
myT.pendown()
# rotate back (if you need to)
mtT.right(90)
This is because you are always facing along a tangent to the circle you just drew and the angle of a tangent to a radius is always 90 degrees (right). This is assuming you just drew the circle, a whole lot of trigonometry involved if you want to calculate the location of the centre of some older arbitrary circle!
I am writing a program (.net) to create a stadium style layout and need to determine the angle of rotation for each polygon compared to the horizontal.
This is so i can construct the contents of the polygon and also rotate this correctly to fit inside.
Given the below image as an example to simulate each variant of the facing direction (indicated by the red line) how could i determine the the rotation angle needed to get the shape to have the red line on top as is already shown by shape 5.
http://i40.tinypic.com/16ifhoo.gif
I have found logic to determine the angle of the points that make up the red line, but I also need to know the rotation to get it back to horizontal.
I'm not sure if i need some central reference point for all polygons to help.
How could I best solve this?
If you know the angle of the red line for some polygon (a, say), then the polygon is on one side or other of that line. So:
Use the average colour of some pixels near the line on both sides to determine which is the case.
If the polygon is above the line, the rotation angle is 180+a.
If the polygon is below the line, the rotation is a.
where above and below correspond to the smaller-angle side and larger-angle sides of the line according to how you measure a.
I would try to calculate the normal vectors on each red line (eg. 0 degrees for polygon 5, 45 degrees for 4, 90 degrees for 3, etc.) and then the angle you need to rotate that normal - and thus the matching polygon - so that the normal "points up" should be very simple.
Unfortunately I don't have the needed formulae available for you off the top of my head, but Googling "normal vector" and/or searching for it on Wikipedia should get you started just fine, I think. Possibly in the direction of the so called 'cross product'.
No central reference point for all polygons should be needed for this (normal direction is not related to absolute coordinates).
sin, cos, tan functions allow you to convert from triangle edge ratio to degrees.
Imagine, one end of red line is at (x1,y1) and other end is at (x2,y2). You can treat red line as hipotenuse of rectangular triangle and use arctan to get degrees.
Ratio between catheti is (x2-x1) / (y2 - y1). Rotation of red line then is arctan((x2-x1) / (y2 - y1)). Watch out for situations when y1-y1 is 0!
Let's try one example from your picture, polygon 6 with coords (55, 65) and (65, 55). Type in google: "arctan((65-55)/(55-65)) in degrees"