Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Last active January 2, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/8344799 to your computer and use it in GitHub Desktop.
Save porglezomp/8344799 to your computer and use it in GitHub Desktop.
Doing my math homework using libgraphicsmath
Math
.DS_Store
#include <graphicsmath0/vec2.h>
#include <graphicsmath0/mat2.h>
#include <stdio.h>
#include <math.h>
// Using libgraphicsmath for vector code
// For more information, see http://github.com/porglezomp/graphicsmath
void pvec(vec2 v) {
printf("<%.2f, %.2f>\n", v.x, v.y);
}
vec2 project(vec2 v, vec2 u) {
return (dot(u, v)/dot(v, v))*v;
}
vec2 rnorm(vec2 v) {
return vec2(v.y, -v.x);
}
vec2 lnorm(vec2 v) {
return vec2(-v.y, v.x);
}
float work63(float d) {
vec2 F = mat2rotation(30) * vec2(15691, 0);
vec2 D (d, 0);
return dot(D, F);
}
int main() {
puts("This program was produces with libgraphicsmath in C++.");
puts("For more information on this program and to see the source, visit https://gist.github.com/porglezomp/8344799");
puts("For more information on libgraphicsmath, visit https://github.com/porglezomp/graphicsmath");
puts(""); // 1
puts("Problem 47");
vec2 u (0, 3);
vec2 v (2, 15);
printf("proj_v u = w1 = ");
pvec(project(v, u));
puts("u = w1 + w2 (below)");
pvec(u - project(v, u));
puts(""); // 2
puts("Problem 49");
u = vec2(3, 2);
v = vec2(6, 4);
puts("mentally: the projection is <3, 2>");
printf("proj_v u = ");
pvec(project(v, u));
puts(""); // 3
puts("Problem 51");
u = vec2(-2, 3);
v = vec2(6, 4);
puts("mentally: the projection is <0, 0>");
printf("proj_v u = ");
pvec(project(v, u));
puts(""); // 4
puts("Problem 53");
v = vec2(2, 6);
puts("Two perpindicular vectors in opposite directions:");
pvec(rnorm(v));
pvec(lnorm(v));
puts(""); // 5
puts("Problem 55");
v = vec2(.5, -.75);
puts("Two perpindicular vectors in opposite directions:");
pvec(rnorm(v));
pvec(lnorm(v));
puts(""); // 6
puts("Problem 57");
vec2 P (0, 0);
vec2 Q (4, 7);
v = vec2(1, 4);
printf("Work = %.3f\n", dot(Q - P, v));
puts(""); // 7
puts("Problem 59");
u = vec2(1245, 2600); // Count of two types of picture frames
v = vec2(12.20, 8.50); // Price of the picture frames
printf("u • v = %.3f\n", dot(u, v));
puts("That represents the money made by selling all the picture frames at the price listed.");
puts("To increas prices by two percent, you'd do u • 1.02v");
printf("u • 1.02v = %.3f\n", dot(u, 1.02*v));
puts(""); // 8
puts("Problem 62");
vec2 g (0, -5400);
vec2 f (-1, 0);
mat2 rot = mat2rotation(10); // Create a 10º ccw rotation matrix
f = rot * f; // Rotate the vector by the matrix
printf("The ground vector is ");
pvec(f);
printf("The projection of gravity onto the ground is ");
pvec(project(f, g));
printf("The force needed is ");
pvec(-project(f, g));
puts(""); // 9
puts("Problem 63");
puts("W(d) = D • F; D = <d, 0>; F = 15691<sin 30º, cos 30º>");
printf("d | %i | %i | %i | %i\n", 0, 200, 400, 800);
printf("W | %3.3f | %3.3f | %3.3f | %3.3f\n", work63(0), work63(200), work63(400), work63(800));
puts(""); // 10
puts("Problem 64");
vec2 F = mat2rotation(30) * vec2(45, 0);
vec2 D (20, 0);
printf("Work is equal to F • D: ");
pvec(dot(F, D));
puts(""); // 11
puts("Problem 65");
F = mat2rotation(30) * vec2(250, 0);
D = vec2(100, 0);
printf("Work is equal to F • D: ");
pvec(dot(F, D));
puts(""); // 12
puts("Problem 66");
F = mat2rotation(20) * vec2(25, 0);
D = vec2(50, 0);
printf("Work is equal to F • D: ");
pvec(dot(F, D));
puts(""); // 13
puts("Problem 67");
F = mat2rotation(25) * vec2(20, 0);
D = vec2(40, 0);
printf("Work is equal to F • D: ");
pvec(dot(F, D));
puts(""); // 14
puts("Problem 68");
F = vec2(25, 0);
D = mat2rotation(20) * vec2(12, 0);
printf("Work is equal to F • D: ");
pvec(dot(F, D));
puts(""); // 15 16 17 18
puts("Problem 69");
puts("True, the dot product is zero <=> they are orthogonal");
puts("Problem 70");
puts("False, the work is the result of a dot product, thus the work is a scalar");
puts("Problem 71");
puts("Orthogonal, v is a right normal to u here ( <y, -x> )");
puts("Problem 72");
puts("(a) theta = 90º (b) 0º ≤ theta < 90º (c) 90º < theta ≤ 180º");
puts(""); // 19
puts("Problem 43");
P = vec2(0, 10); Q = vec2(7, 3);
printf("Vec from P to Q is ");
pvec(Q - P);
puts(""); // 20
puts("Problem 49");
puts("Not possible here, requires graphics"); // TODO: Draw this on paper
puts(""); // 21
puts("Problem 53");
u = vec2(-1, -3);
v = vec2(-3, 6);
printf("(a) u + v = "); pvec(u + v);
printf("(b) u - v = "); pvec(u - v);
printf("(c) 3u = "); pvec(3 * u);
printf("(d) 2v + 5u = "); pvec(2 * v + 5 * u);
puts(""); // 22
puts("Problem 57");
u = vec2(2, -1);
v = vec2(5, 3);
printf("(a) u + v = "); pvec(u + v);
printf("(b) u - v = "); pvec(u - v);
printf("(c) 3u = "); pvec(3 * u);
printf("(d) 2v + 5u = "); pvec(2 * v + 5 * u);
puts(""); // 23
puts("Problem 63");
u = vec2(6, -5);
v = vec2(10, 3);
printf("4u + 5v = "); pvec(4*u + 5*v);
puts(""); // 24
puts("Problem 65");
puts("<0, -1>"); // Come on you guys, this problem is completely trivial!
puts(""); // 25
puts("Problem 72");
v = vec2(4, -1);
printf("v = "); pvec(v);
float mag = length(v);
printf("||v|| = %f\n", mag);
v /= mag; // Make v a unit vector
printf("theta (radians) = %.3f\n", asinf(v.y));
printf("%.3f * <sin %.3f, cos %.3f>\n", mag, asinf(v.y), asinf(v.y));
puts(""); // 26
puts("Problem 74"); // TODO: Draw the graph
u = mat2rotation(-12) * vec2(8, 0);
v = mat2rotation(82) * vec2(12, 0);
printf("u = "); pvec(u);
printf("v = "); pvec(v);
vec2 res = u + v;
printf("u + v = "); pvec(res);
printf("||u + v|| = %f\n", length(res));
printf("theta (radians) = %f\n", atan2(res.y, res.x));
}
all: Math
Math : main.cpp
g++ main.cpp -o Math -lgraphicsmath-0.7 -I$(HOME)/usr/include -L$(HOME)/usr/lib
This program was produces with libgraphicsmath in C++.
For more information on this program and to see the source, visit https://gist.github.com/porglezomp/8344799
For more information on libgraphicsmath, visit https://github.com/porglezomp/graphicsmath
Problem 47
proj_v u = w1 = <0.39, 2.95>
u = w1 + w2 (below)
<-0.39, 0.05>
Problem 49
mentally: the projection is <3, 2>
proj_v u = <3.00, 2.00>
Problem 51
mentally: the projection is <0, 0>
proj_v u = <0.00, 0.00>
Problem 53
Two perpindicular vectors in opposite directions:
<6.00, -2.00>
<-6.00, 2.00>
Problem 55
Two perpindicular vectors in opposite directions:
<-0.75, -0.50>
<0.75, 0.50>
Problem 57
Work = 32.000
Problem 59
u • v = 37289.000
That represents the money made by selling all the picture frames at the price listed.
To increas prices by two percent, you'd do u • 1.02v
u • 1.02v = 38034.781
Problem 62
The ground vector is <-0.98, -0.17>
The projection of gravity onto the ground is <-923.45, -162.83>
The force needed is <923.45, 162.83>
Problem 63
W(d) = D • F; D = <d, 0>; F = 15691<sin 30º, cos 30º>
d | 0 | 200 | 400 | 800
W | 0.000 | 2717761.000 | 5435522.000 | 10871044.000
Problem 64
Work is equal to F • D: <779.42, 779.42>
Problem 65
Work is equal to F • D: <21650.63, 21650.63>
Problem 66
Work is equal to F • D: <1174.62, 1174.62>
Problem 67
Work is equal to F • D: <725.05, 725.05>
Problem 68
Work is equal to F • D: <281.91, 281.91>
Problem 69
True, the dot product is zero <=> they are orthogonal
Problem 70
False, the work is the result of a dot product, thus the work is a scalar
Problem 71
Orthogonal, v is a right normal to u here ( <y, -x> )
Problem 72
(a) theta = 90º (b) 0º ≤ theta < 90º (c) 90º < theta ≤ 180º
Problem 43
Vec from P to Q is <7.00, -7.00>
Problem 49
Not possible here, requires graphics
Problem 53
(a) u + v = <-4.00, 3.00>
(b) u - v = <2.00, -9.00>
(c) 3u = <-3.00, -9.00>
(d) 2v + 5u = <-11.00, -3.00>
Problem 57
(a) u + v = <7.00, 2.00>
(b) u - v = <-3.00, -4.00>
(c) 3u = <6.00, -3.00>
(d) 2v + 5u = <20.00, 1.00>
Problem 63
4u + 5v = <74.00, -5.00>
Problem 65
<0, -1>
Problem 72
v = <4.00, -1.00>
||v|| = 4.123106
theta (radians) = -0.245
4.123 * <sin -0.245, cos -0.245>
Problem 74
u = <7.83, -1.66>
v = <1.67, 11.88>
u + v = <9.50, 10.22>
||u + v|| = 13.950152
theta (radians) = 0.822138
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment