Basic trigonometry isn't working correctly in python - python

For a bit of background, this is the game I'm trying to draw in an isometric style.
I'm just trying to get the correct calculations instead of doing it in a hacky way, but one part isn't working, and I'm not quite sure about the other part (I'll save that for another question later on).
So, forgetting the little squares inbetween, the board is made up of n lenels. I'd like to be able to calculate the coordinates for these so I can do further calculations on them, but the trig isn't working, and to the best of my knowledge (bear in mind I last did it years ago), it should be.
This is the simple code to draw the first square:
import turtle
import math
iso_angle = 20
grid_length = 100
turtle.right(iso_angle)
turtle.forward(grid_length)
turtle.right(180 - iso_angle * 2)
turtle.forward(grid_length)
turtle.right(iso_angle * 2)
turtle.forward(grid_length)
turtle.right(180 - iso_angle * 2)
turtle.forward(grid_length)
Using sohcahtoa, I thought I'd be able to calculate the width and height of the resulting square, since it's effectively made up of 4 triangles, and I can double the height of one of the triangles.
#s = o / h
#sin(iso_angle) = o / grid_length
#o = sin(iso_angle) * grid_length
height = 2 * sin(iso_angle) * grid_length
width = 2 * cos(iso_angle) * grid_length
However, when I move the turtle down by height, it doesn't fall on the edge of the square. It doesn't even move in a multiple of the distance of the edge, it just seems to end up some random distance. Swapping with width doesn't work either.
Where abouts am I going wrong with this?

As stated in the comments you need to convert to radians which can be done with the
math.radians()
function. So in practice you would end with something like
height = 2 * sin(math.radians(iso_angle)) * grid_length
width = 2 * cos(math.radians(iso_angle)) * grid_length

The cursor module (turtle) takes angles in degrees.
The sin() and cos() math functions take angles in radians. You must convert them. Fortunetly, Python includes convenient functions to do that in the math module:
height = 2 * sin(radians(iso_angle)) * grid_length
Hope this helps.

Related

Know difference between two angles with only their sine and cosine?

Hi recently I was writing a program with a bunch dots that the player had to avoid. The dots changed direction, but can't turn too much in a given timeframe.
I had a method of doing this but was inefficient as I had to arcsine, knowing the cosed angle and sined angle, then sine and cosine that angle. I thought of just returning the cosine and sined angle, but theres one problem. Once I received the cosine and sine, I need too know if it is too different from my current state.
With the angle this would be easy as I would just have too see the difference, here's a model program,(the code I currently have uses the angle, and isn't very helpful). I have tried graphing sine and cosine and trying to observe any patterns, none obvious showed up.
import math
def sendTargRot():
#I actually use a fairly long method to find it's current rotation, but a random.random() is a fair replacement, so in my real thing there would be no angle calculation
pretendAngle = math.pi*random.random()-math.pi
pretendCosedX = math.cos(pretendAngle)
pretendSinedX = math.sin(pretendAngle)
def changeDotAngle():
targRot = sendTargRot
#make sure targRot is not too much from current rotation
#do stuff with the angle, change dot's rotation
If you want to just give me an algorithm without code that is just as acceptable!
EDIT: I can't really change sendTargRot so it gives a proper rotation, because that would require me knowing the current angle, and really it's just moving the problem.
To get angle between two vectors you can use atan2 function
angle = Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y)
if you already have got cosines and sines (the same as coordinates on unit circle):
angle = Math.atan2(cos(a) * sin(b) - sin(a) * cos(b), cos(a) * cos(b) + sin(a) * sin(b))
This approach gives angle needed to rotate a until it coincides with b accounting for direction (in range -Pi..Pi)

Python turtle circle function

So I'm reading a book to learn python and I got to a part about the module turtle.
So after explaining it, it gives you some exercises.
One of them is to define a function that creates regular polygons.
I got this to work.
import turtle
bob = turtle.Turtle()
def polygon(t, l, n):
angle = 360/n
for i in range(n):
t.fd(l)
t.lt(angle)
polygon(bob, 40, 5)
For example this draws a regular pentagon.
The next exercise asks you to draw a "circle" changing the number of sides of the polygon.
The problem is that sometimes it doesn't work and the polygon/circle doesn't close.
I tried to find the solution by changing lots of time both the lenght and the number of sides or only one of the two but I didn't succeed.
For example, lenght = 10 and n°sides = 140 doesn't work, instead lenght = 20 and n°sides = 120 works.
Can someone explain, please?
Found solution.
Being a beginner I forgot about integers and floats.
That's why the "circle" wasn't closing.
Your code works fine in Python 3 but didn't close the polygon in Python 2 due to the difference in how division works. The fix is to simply use 360.0 instead of 360 and then it works fine in both:
from turtle import Turtle, Screen
def polygon(t, l, n):
angle = 360.0 / n
for _ in range(n):
t.fd(l)
t.lt(angle)
bob = Turtle()
polygon(bob, 10, 140)
screen = Screen()
screen.exitonclick()
Python turtle's own circle() method actually draws polygons with the default assumption that 60 sides is sufficient to look like a circle on the screen. Unless the circle is very small (then it uses fewer sides) or the user insists on more (or less) sides via the steps argument.
Try putting 360.0 instead of 360, because the initial value of Python is in integers.
We want to convert it into decimals, that's why we put the .0 after the 360.

Is this Python script correct?

I have a Programming Assignment in my class and I tried my best to understand everything but I want to make sure that my script is correct before I submit it. I need to find the area of a triangle which is given by the formula:
Area = 1/2 * Base * Height
This is my answer:
# The base and height of this triangle are inches
base = 12.0
height = 16.0
# The area of a triangle is 1/2 * base * height
area = 1/2 * base * height
print (area)
Is my code correct?
Yes, this code is correct, by assigning the values to different variables it is clear what you have done.

I need help forming a circle with cubes, useing blender 2.69 with python engine

Please forgive me, but I only really know how to somewhat code in VB, and python is not what I'm used to. I did try to see if other people have made and or shown an algorithm that I'm trying to accomplish.
I have a visualizer design in my head and What I have been trying to do is get a number of cubes (variable input for now) to be placed a certain distance (maybe 5-10 blender units) from the center of the scene and angle the faces so that there will be one face pointing to the center and one face pointing the opposite direction. I'm trying to just start with 10 cubes because I feel like it will be a fair number to hopefully show a circle shape.
I made an image to help describe what I am trying to do:
All I have been able to figure out so far is that I need to add a cube with a certain rotation, and that rotation needs to be stepped for each cube. so a small equation is needed, something like this.
(10) (36)
360 / numberOfCubes = steppedAngle
That's all I have been able to figure out because I don't know how to program python to do such.
Any help is greatly appreciated, and will be credited in the final render.
Update: 1
Thanks to the help from the answer below, I finally got it to work how i wanted.
img http://vvcap.net/db/bKKUz3Uw4WUqL_WVDU0j.png
and here is the code written in help from the answer below.
'
import bpy
import math
##num of cubes
n = 10
##distange from center
radius = 7
for i in range(1, n + 1):
angle = (i - 1) * math.pi * 2 / n
xcoord=(radius * math.cos(angle))
ycoord=(radius * math.sin(angle))
bpy.ops.mesh.primitive_cube_add(location=(xcoord,ycoord,0),rotation=(0,0,angle))
'
Let's start with cubes in a circle, and we will work our way from there.
You have N cubes, and you want to place them in a circle of radius R around the center of the universe (0,0,0).
Applying basic trigonometry:
Each cube is on one of the radius of the circle, when you divide the circle by N. Therefore, your first cube is at 0 rad, your second cube is at 2*pi/N rad, your third cube is at 2 * 360/N rad, ... your N cube is at (N-1) * 2*pi/N rad. Now we have a formula:
Location of the cube in the circle = (i - 1) * 2*pi/N in radians, for each i from 1 to N.
Now, the location in space coordinates is (r*cos(angle), r*sin(angle), 0) for a circle that is placed on the XY plane and it's center is on (0,0,0).
My Blender Python is very rusty, so I won't provide you a complete solution, but it should be this way:
import math
for i in range(1, N + 1):
angle = (i - 1) * math.pi * 2 / N
x_coord = radius * math.cos(angle)
y_coord = radius * math.sin(angle)
z_coord = 0
cube = place_cube(x_coord, y_coord, z_coord)
This will place the cubes on the right coordinates, but it won't turn them the right way. Fortunately, you can rotate each cube by angle, and get it in the right orientation. So you can do:
import math
for i in range(1, N + 1):
angle = (i - 1) * math.pi * 2 / N
x_coord = radius * math.cos(angle)
y_coord = radius * math.sin(angle)
z_coord = 0
cube = place_cube(x_coord, y_coord, z_coord)
cube.rotate_around_z(angle)
I have not provided the place_cube and rotate_around_z functions because I hardly remember the Blender Python api, but it shouldn't be too hard.

Python (pygame) Sin, Cos and Tan

I noticed there was a sin, cos and tan function in Python.
So, I thought I would use these to make a way of aiming in my game, unfortunately, the word description of sin,cos,tan,asin,acos and atan are very confusing.
I know how to do all the sin, cos and tan rules from school, I just need to apply them to the code. So, here's what I need to do, I just need to know which one I must use:
I have
The Angle
The Hypotenuse
(I'm just keeping that the value of how far I want the object to travel before I blit it again)
From the angle, I want to work out either/both the opposite and adjacent.
The hypotenuse is going to be sin/asin and cos/acos. Which one? I don't know.
How to I input my numbers? Do I just do aim = cos(angle,hyp) or do I have to apply some other calculations?
The formulae are:
adjacent = hypothenuse * math.cos(angle)
opposite = hypothenuse * math.sin(angle)
where angle is in radians.
Your wording is a bit confusing... but what I understand is that you have a point in the 2D space and you want to advance it a particular distance (hypotenuse) aiming a specified angle above the horizon. If so:
newX = oldX + dist * cos(angle)
newY = oldY + dist * sin(angle)
That assumes that angle is in radians, that the Y axis is positive upwards and that the angle is 0 aiming to the right and PI/2 to the top. If these are not the case you may need to wiggle the signs a little.

Categories