Last active
December 25, 2015 18:09
-
-
Save saulshanabrook/7018535 to your computer and use it in GitHub Desktop.
Vector Adventure Lab Finished Product: http://f.cl.ly/items/2U1q1r1o2d1a0q2I453S/12.10.04%20Vector%20Adventure%20Lab.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Included in LaTeX document, but also I put it here for better code highlighting | |
import math | |
import operator | |
import itertools | |
import numpy | |
def polar_to_cartesian(point): | |
r, theta = point | |
theta = 90 - theta | |
x = r * math.cos(math.radians(theta)) | |
y = r * math.sin(math.radians(theta)) | |
return (x, y) | |
def cartesian_to_polar(point): | |
x, y = point | |
theta = 90 - numpy.angle(complex(x,y), deg=True) | |
r = numpy.hypot(x, y) | |
return (r, theta) | |
def add_points(points): | |
return sum(map(numpy.array, map(polar_to_cartesian, points))) | |
def draw_vector(old_coordinate, polar_vector, label, style='', position='above'): | |
old_coordinate = numpy.around(old_coordinate, 2) | |
cartesian_vector = numpy.around(polar_to_cartesian(polar_vector), 2) | |
new_coordinate = numpy.around(old_coordinate + cartesian_vector, 2) | |
halfway_coordinate = numpy.around(old_coordinate + (cartesian_vector / 2), 2) | |
polar_vector = numpy.around(polar_vector, 2) | |
print(r''' | |
\addplot[ | |
quiver={{ | |
u={cartesian_v[0]}, | |
v={cartesian_v[1]}, | |
every arrow/.append style={{ | |
line width=2pt,{style} | |
}}, | |
}}, | |
-stealth | |
] | |
coordinates {{ ({old_c[0]}, {old_c[1]})}}; | |
\addlegendentry{{$\vec{{{label}}}={polar_v[0]}\unit{{ft}}; {polar_v[1]}\degree$}} | |
\node[coordinate, pin={position}:$\vec{{{label}}}$] | |
at (axis cs:{halfway_c[0]},{halfway_c[1]}) {{}}; | |
'''.format(old_c=old_coordinate, cartesian_v=cartesian_vector, | |
polar_v=polar_vector, new_c=new_coordinate, label=label, | |
style=style, position=position, halfway_c=halfway_coordinate)) | |
polar_vectors = [ | |
(0, 0), | |
(17, 290), | |
(24, 155), | |
(24, 65), | |
(8.5, 290)] | |
vector_names = ['A', 'B', 'C', 'D'] | |
for n in range(1, len(polar_vectors)): | |
old_coordinate = add_points(polar_vectors[:n]) | |
label = vector_names[n - 1] | |
draw_vector(old_coordinate, polar_vectors[n], label=label) | |
displacement_vector = numpy.array((12.5 * 2.08, 28)) | |
draw_vector(numpy.array((0, 0)), | |
displacement_vector, | |
label='displacement', | |
position='left') | |
last_calculate_point = add_points(polar_vectors) | |
displacement_to_last_calculate_vector = polar_to_cartesian(displacement_vector) - last_calculate_point | |
draw_vector(last_calculate_point, | |
cartesian_to_polar(displacement_to_last_calculate_vector), | |
'error', | |
'style={loosely dashed}', | |
'right') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[11pt, oneside]{article} % use "amsart" instead of "article" for AMSLaTeX format | |
\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots. | |
\geometry{letterpaper} % ... or a4paper or a5paper or ... | |
\geometry{landscape} % Activate for for rotated page geometry | |
%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent | |
\usepackage{graphicx} % Use pdf, png, jpg, or eps§ with pdflatex; use eps in DVI mode | |
% TeX will automatically convert eps --> pdf in pdflatex | |
\usepackage{amssymb} | |
\usepackage{python} | |
\usepackage{units} | |
\usepackage{pgfplots} | |
\usepgfplotslibrary{polar} | |
\newcommand{\degree}{\ensuremath{^\circ}} | |
\usepgfplotslibrary{units} | |
\title{Vector Adventure Lab} | |
\author{Saul Shanabrook} | |
%\date{} % Activate to display a given date or no date | |
\pgfplotsset{width=6in,compat=1.6} | |
\begin{document} | |
\maketitle | |
\begin{figure} | |
\centering | |
\begin{tikzpicture} | |
\begin{axis}[use units, | |
scale only axis, | |
xmin=-20,xmax=20, | |
axis y line*=left, | |
title=North, | |
xlabel=South,ylabel=West, | |
x unit=ft, y unit=ft, | |
legend style={ | |
cells={anchor=east}, | |
legend pos=outer north east, | |
}] | |
\begin{python} | |
import math | |
import operator | |
import itertools | |
import numpy | |
def polar_to_cartesian(point): | |
r, theta = point | |
theta = 90 - theta | |
x = r * math.cos(math.radians(theta)) | |
y = r * math.sin(math.radians(theta)) | |
return (x, y) | |
def cartesian_to_polar(point): | |
x, y = point | |
theta = 90 - numpy.angle(complex(x,y), deg=True) | |
r = numpy.hypot(x, y) | |
return (r, theta) | |
def add_points(points): | |
return sum(map(numpy.array, map(polar_to_cartesian, points))) | |
def draw_vector(old_coordinate, polar_vector, label, style='', position='above'): | |
old_coordinate = numpy.around(old_coordinate, 2) | |
cartesian_vector = numpy.around(polar_to_cartesian(polar_vector), 2) | |
new_coordinate = numpy.around(old_coordinate + cartesian_vector, 2) | |
halfway_coordinate = numpy.around(old_coordinate + (cartesian_vector / 2), 2) | |
polar_vector = numpy.around(polar_vector, 2) | |
print(r''' | |
\addplot[ | |
quiver={{ | |
u={cartesian_v[0]}, | |
v={cartesian_v[1]}, | |
every arrow/.append style={{ | |
line width=2pt,{style} | |
}}, | |
}}, | |
-stealth | |
] | |
coordinates {{ ({old_c[0]}, {old_c[1]})}}; | |
\addlegendentry{{$\vec{{{label}}}={polar_v[0]}\unit{{ft}}; {polar_v[1]}\degree$}} | |
\node[coordinate, pin={position}:$\vec{{{label}}}$] | |
at (axis cs:{halfway_c[0]},{halfway_c[1]}) {{}}; | |
'''.format(old_c=old_coordinate, cartesian_v=cartesian_vector, | |
polar_v=polar_vector, new_c=new_coordinate, label=label, | |
style=style, position=position, halfway_c=halfway_coordinate)) | |
polar_vectors = [ | |
(0, 0), | |
(17, 290), | |
(24, 155), | |
(24, 65), | |
(8.5, 290)] | |
vector_names = ['A', 'B', 'C', 'D'] | |
for n in range(1, len(polar_vectors)): | |
old_coordinate = add_points(polar_vectors[:n]) | |
label = vector_names[n - 1] | |
draw_vector(old_coordinate, polar_vectors[n], label=label) | |
displacement_vector = numpy.array((12.5 * 2.08, 28)) | |
draw_vector(numpy.array((0, 0)), | |
displacement_vector, | |
label='displacement', | |
position='left') | |
last_calculate_point = add_points(polar_vectors) | |
displacement_to_last_calculate_vector = polar_to_cartesian(displacement_vector) - last_calculate_point | |
draw_vector(last_calculate_point, | |
cartesian_to_polar(displacement_to_last_calculate_vector), | |
'error', | |
'style={loosely dashed}', | |
'right') | |
\end{python} | |
\end{axis} | |
\begin{axis}[ | |
scale only axis, | |
xmin=-20,xmax=20, | |
axis y line*=right, | |
axis x line=none, | |
ylabel=West, | |
yticklabel=\empty]; | |
\end{axis} | |
\end{tikzpicture} | |
\end{figure} | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment