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!
Related
I am looking for an algorithm for the following problem:
Given:
A 3D triangle mesh. The mesh represents a part of the surface of the earth.
A polyline (a connected series of line segments) whose vertices are always on an edge or on a vertex of a triangle of the mesh. The polyline represents the centerline of a road on the surface of the earth.
I need to calculate and display the road i.e. add half of the road's width on each side of the center line, calculate the resulting vertices in the corresponding triangles of the mesh, fill the area of the road and outline the sides of the road.
What is the simplest and/or most effective strategy to do this? How do I store the data of the road most efficiently?
I see 2 options here:
render thick polyline with road texture
While rendering polyline you need TBN matrix so use
polyline tangent as tangent
surface normal as normal
binormal=tangent x normal
shift actual point p position to
p0=p+d*binormal
p1=p-d*binormal
and render textured line (p0,p1). This approach is not precise match to surface mesh so you need to disable depth or use some sort of blending. Also on sharp turns it could miss some parts of a curve (in that case you can render rectangle or disc instead of line.
create the mesh by shifting polyline to sides by half road size
This produces mesh accurate road fit, but due to your limitations the shape of the road can be very distorted without mesh re-triangulation in some cases. I see it like this:
for each segment of road cast 2 lines shifted by half of road size (green,brown)
find their intersection (aqua dots) with shared edge of mesh with the current road control point (red dot)
obtain the average point (magenta dot) from the intersections and use that as road mesh vertex. In case one of the point is outside shared mesh ignore it. In case both intersections are outside shared edge find closest intersection with different edge.
As you can see this can lead to serious road thickness distortions in some cases (big differences between intersection points, or one of the intersection points is outside surface mesh edge).
If you need accurate road thickness then use the intersection of the casted lines as a road control point instead. To make it possible either use blending or disabling Depth while rendering or add this point to mesh of the surface by re-triangulating the surface mesh. Of coarse such action will also affect the road mesh and you need to iterate few times ...
Another way is use of blended texture for road (like sprites) and compute the texture coordinate for the control points. If the road is too thick then thin it by shifting the texture coordinate ... To make this work you need to select the most far intersection point instead of average ... Compute the real half size of the road and from that compute texture coordinate.
If you get rid of the limitation (for road mesh) that road vertex points are at surface mesh segments or vertexes then you can simply use the intersection of shifted lines alone. That will get rid of the thickness artifacts and simplify things a lot.
I have a straight line which intersects a convex polygon in 2D plane. There exists a circle with constant radius. The center of circle is moving on this line. So at first the polygon and circle don't intersect with each other, as the circle gets closer to the polygon the intersection increases and then decreases as they go further from each other. I want to prove the area of the intersection of the convex polygon and circle doesn't have local minima(as the circle moves on the line).
Interesting problem. Please post solution once you find it. My approach would be to take a similar route to Fortunes algorithm to build a Voronoi graph - meaning I would consider "events" that are happening when the circle traverses a convex polygon.
Basically to better understand the problem, consider the restriction that the circle is traveling on straight line - why is that important - look at counter examples. Then look when will this fail if poly is not convex?
The events that I would consider would be an entry/exit of a poly vertex into circle, and entry exit of an poly edge from/into the circle. Then keep track of area increasing or decreasing through each event, and show that it is necessarily monotonic.
Is there a way to draw a simple circle with given radius and center point in irrlicht?
You can use driver->draw2DPolygon() method.
Simply pass vertexCount to be high enough for your size of shape (the smaller circle on the screen, the lower value you can pass and it won't be noticeable that it has edges).
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 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"