Skip to content

Instantly share code, notes, and snippets.

@eric-vader
Last active August 29, 2015 14:24
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 eric-vader/4eb29ba46a9cfadefee1 to your computer and use it in GitHub Desktop.
Save eric-vader/4eb29ba46a9cfadefee1 to your computer and use it in GitHub Desktop.
//============================================================================
// Name : test1.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <math.h>
using namespace std;
void minusVector(float* result, float* a, float* b) {
result[0] = a[0] - b[0];
result[1] = a[1] - b[1];
result[2] = a[2] - b[2];
}
void inputVector(float* v) {
for (int x = 0; x < 3; x++) {
cin >> v[x];
}
}
void displayVector(float* v) {
for (int x = 0; x < 3; x++) {
cout << v[x] << ",";
}
cout << endl;
}
float dot(float* a, float* b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
float lengthVector(float* v) {
return sqrt(dot(v, v));
}
void surfaceNormal(float* normal, float* p1, float* p2, float* p3) {
float u[3];
float v[3];
minusVector(u, p2, p1);
minusVector(v, p3, p1);
displayVector(u);
displayVector(v);
normal[0] = u[1] * v[2] - u[2] * v[1];
normal[1] = u[2] * v[0] - u[0] * v[2];
normal[2] = u[0] * v[1] - u[1] * v[0];
}
void normalize(float* v) {
float length = lengthVector(v);
v[0] = v[0] / length;
v[1] = v[1] / length;
v[2] = v[2] / length;
}
int main() {
// 3 points from std normal
float p1[3]={0,0,1};
float p2[3]={1,0,0};
float p3[3]={0,1,0};
//inputVector(p1);
//inputVector(p2);
//inputVector(p3);
float normalFromInfinity[3];
surfaceNormal(normalFromInfinity, p1, p2, p3);
displayVector(normalFromInfinity);
normalize(normalFromInfinity);
displayVector(normalFromInfinity);
float normalFromOrigin[3];
surfaceNormal(normalFromOrigin, p1, p3, p2);
displayVector(normalFromOrigin);
normalize(normalFromOrigin);
displayVector(normalFromOrigin);
/*
1,0,-1,
0,1,-1,
1,1,1,
0.57735,0.57735,0.57735,
0,1,-1,
1,0,-1,
-1,-1,-1,
-0.57735,-0.57735,-0.57735,
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment