Skip to content

Instantly share code, notes, and snippets.

@mindcrime
Last active June 3, 2023 23:24
Show Gist options
  • Save mindcrime/71825c740602f5767945b0a4561085db to your computer and use it in GitHub Desktop.
Save mindcrime/71825c740602f5767945b0a4561085db to your computer and use it in GitHub Desktop.
Exploring Exercise 2.39 in "Math for Programmers" by P. Orland
'''
Created on Jun 3, 2023
@author: prhodes
'''
from ch02.vector_drawing import *
from math import sqrt, pi, cos, sin, tan, asin, acos, atan, atan2, isclose
def main():
print( "Hello, Exercise 2.39\n" )
# Assignment:
# Find another point in the plane with the same tangent as
# theta, namely -3/2. Use Python's implementation of arctangent
# math.atan, to find the value of this angle
print( "atan(-3/2): ", atan(-3/2), ", in degrees: ", rads_to_degrees(atan(-3/2) ), "\n" )
print( "tan(-0.982793723247329): ", tan(-0.982793723247329), "\n" )
polar_23 = cart_to_polar( (2,-3) )
angle_23 = polar_23[1]
print( "Angle_23: ", angle_23, "\n" )
print( "Angle_23 in rads: ", degrees_to_rads( angle_23 ), "\n" )
print( "tan(", angle_23, "): ", tan(angle_23), "\n" )
print( "tan(", degrees_to_rads(angle_23), "): ", tan(degrees_to_rads(angle_23)), "\n" )
points = []
for x in range( -5, 6 ):
for y in range( -5, 6 ):
polar = cart_to_polar( (x,y ) )
angle = degrees_to_rads( polar[1] )
if( x == 2 and y == -3 ):
print( "angle: ", angle, "\n" )
if( isclose( angle, atan(-3/2), abs_tol=0.01) ):
print( "OK" )
points.append( (x,y) )
print( "Points: ", points, "\n" )
def degrees_to_rads( degrees ):
# 1° × π/180 = 0.01745rad
rads = degrees * (pi/180)
return rads
def rads_to_degrees( rads ):
# 1rad × 180/π = 57.296°
degrees = rads * (180/pi)
return degrees
def cart_to_polar( cart ):
x, y = cart[0], cart[1]
angle = atan2( y, x )
return ( length(cart), angle * (180/pi) )
def polar_to_cart( polar ):
distance = polar[0]
angle_degrees = polar[1]
print( "angle_degrees: ", angle_degrees, "\n" )
angle_rads = angle_degrees * ( pi / 180 )
print( "distance: ", distance, "\n" )
print( "angle_rads: ", angle_rads, "\n" )
cos_angle = cos(angle_rads)
sin_angle = sin(angle_rads)
print( "cos_angle: ", cos_angle, "\n" )
print( "sin_angle: ", sin_angle, "\n" )
x = cos_angle * distance
y = sin_angle * distance
print( "x: ", x, "\n" )
print( "y: ", y, "\n" )
return (x,y)
def perimeter( vectors ):
num_items = len( vectors )
perimeter = 0
for i in range( 0, num_items ):
print( i, "\n" )
if( i == (num_items -1 )):
perimeter = perimeter + (distance( vectors[num_items-1], vectors[0]))
else:
perimeter = perimeter + (distance(vectors[i], vectors[i+1]))
return perimeter
def distance( v1, v2 ):
displacement = subtract( v1, v2 )
distance = sqrt( (displacement[0]**2) + (displacement[1]**2 ) )
return distance
def translate( translation, vectors ):
# a "one liner" version
return [ add(translation,v) for v in vectors ]
def subtract( v1, v2 ):
return ( v1[0] - v2[0], v1[1] - v2[1] )
def add( v1, v2 ):
return ( v1[0] + v2[0], v1[1] + v2[1] )
def scale( v, scalar ):
return (v[0]*scalar, v[1]*scalar)
def length( v ):
return sqrt( v[0]**2 + v[1]**2 )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment