0

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?

Community
  • 1
  • 1
Peter
  • 3,186
  • 3
  • 26
  • 59
  • 4
    `sin` and `cos` expect an angle in radians, not degrees. – hobbs Oct 06 '15 at 21:26
  • @hobbs: you beat me to it. It was an educated guess anyway, so I had to verify on https://docs.python.org/2/library/math.html first. – Jongware Oct 06 '15 at 21:27
  • Ahh, I'd given that a thought but accidentally converted radian to degrees instead of the other way around. It seems to be working now anyway, thanks, should I leave this question up? – Peter Oct 06 '15 at 21:30
  • Possible duplicate of [Unusual Math with incorrect results?](http://stackoverflow.com/questions/8691800/unusual-math-with-incorrect-results) – Serge Ballesta Oct 06 '15 at 21:35
  • @Peter: It is not an exact duplicate, but the cause is the same, so it could be closed – Serge Ballesta Oct 06 '15 at 21:37

2 Answers2

1

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
karakfa
  • 66,216
  • 7
  • 41
  • 56
1

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.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39