Triangle normal
in
Programming Questions
•
2 years ago
Hi guys,
I am trying to understand what is wrong with the cross product. It is giving me some funky result when I normalize it. I guess its a simple one...really thankful if somebody could help. Here is the code:
Thanks a lot :)
buzz
// peasyCam library
import peasy.*;
// orbit
PeasyCam cam;
void setup()
{
size(600, 600, P3D);
smooth();
colorMode(HSB, 6, 100, 100); // mode, rangeHue, rangeSat, rangeBright
cam = new PeasyCam(this, 100); // qto maior menor o obj
cam.setMinimumDistance(10); // close zoom
cam.setMaximumDistance(10000); // far zoom
}
void draw()
{
background(6);
//translate(width/2, height/2, 0);
//rotateY(frameCount*0.01);
PVector a = new PVector(100, 100, 0);
PVector b = new PVector(150, 100, 0);
PVector c = new PVector(200, 200, 0);
strokeWeight(5);
stroke(0);
point(a.x, a.y, a.z);
point(b.x, b.y, b.z);
point(c.x, c.y, c.z);
// 1. calculate the cross product
// PVector n = new PVector((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x));
// stroke(0, 100, 100);
// strokeWeight(5);
// point(n.x, n.y, n.z);
// 1.1 cross product check
PVector n = a.cross(b);
//n.normalize(); ?????????????????????????????????
println(n);
stroke(0, 100, 100);
strokeWeight(15);
point(n.x, n.y, n.z);
// 2. find the centroid
PVector g = new PVector( (a.x+b.x+c.x)/3, (a.y+b.y+c.y)/3, (a.z+b.z+c.z)/3 );
stroke(2, 100, 100);
strokeWeight(5);
point(g.x, g.y, g.z);
float d = g.dist(n);
println(d);
// 3. draw the normal
strokeWeight(1);
stroke(0, 100, 100);
// line(g.x, g.y, g.z, n.x, n.y, n.z);
line(g.x, g.y, g.z, n.x, n.y, n.z);
// 4. pvector that follows the mouse - will be replaced by the sun
PVector m = new PVector(mouseX, mouseY, 300);
stroke(0);
line(g.x, g.y, g.z, m.x, m.y, m.z);
// 5. calcullate the angle between sun and normal vector
float angle = PVector.angleBetween(n, m);
//println(degrees(angle));
// 6. map the angle to a color value anf fill the triangle to that value
float mapping = map(angle, 0, 180, 0, 255); //possible angles to possible colours
//println(mapping);
strokeWeight(1);
stroke(0);
fill(mapping, 100, 100);
beginShape(TRIANGLES);
vertex(a.x, a.y, a.z);
vertex(b.x, b.y, b.z);
vertex(c.x, c.y, c.z);
endShape();
}
I am trying to understand what is wrong with the cross product. It is giving me some funky result when I normalize it. I guess its a simple one...really thankful if somebody could help. Here is the code:
Thanks a lot :)
buzz
// peasyCam library
import peasy.*;
// orbit
PeasyCam cam;
void setup()
{
size(600, 600, P3D);
smooth();
colorMode(HSB, 6, 100, 100); // mode, rangeHue, rangeSat, rangeBright
cam = new PeasyCam(this, 100); // qto maior menor o obj
cam.setMinimumDistance(10); // close zoom
cam.setMaximumDistance(10000); // far zoom
}
void draw()
{
background(6);
//translate(width/2, height/2, 0);
//rotateY(frameCount*0.01);
PVector a = new PVector(100, 100, 0);
PVector b = new PVector(150, 100, 0);
PVector c = new PVector(200, 200, 0);
strokeWeight(5);
stroke(0);
point(a.x, a.y, a.z);
point(b.x, b.y, b.z);
point(c.x, c.y, c.z);
// 1. calculate the cross product
// PVector n = new PVector((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x));
// stroke(0, 100, 100);
// strokeWeight(5);
// point(n.x, n.y, n.z);
// 1.1 cross product check
PVector n = a.cross(b);
//n.normalize(); ?????????????????????????????????
println(n);
stroke(0, 100, 100);
strokeWeight(15);
point(n.x, n.y, n.z);
// 2. find the centroid
PVector g = new PVector( (a.x+b.x+c.x)/3, (a.y+b.y+c.y)/3, (a.z+b.z+c.z)/3 );
stroke(2, 100, 100);
strokeWeight(5);
point(g.x, g.y, g.z);
float d = g.dist(n);
println(d);
// 3. draw the normal
strokeWeight(1);
stroke(0, 100, 100);
// line(g.x, g.y, g.z, n.x, n.y, n.z);
line(g.x, g.y, g.z, n.x, n.y, n.z);
// 4. pvector that follows the mouse - will be replaced by the sun
PVector m = new PVector(mouseX, mouseY, 300);
stroke(0);
line(g.x, g.y, g.z, m.x, m.y, m.z);
// 5. calcullate the angle between sun and normal vector
float angle = PVector.angleBetween(n, m);
//println(degrees(angle));
// 6. map the angle to a color value anf fill the triangle to that value
float mapping = map(angle, 0, 180, 0, 255); //possible angles to possible colours
//println(mapping);
strokeWeight(1);
stroke(0);
fill(mapping, 100, 100);
beginShape(TRIANGLES);
vertex(a.x, a.y, a.z);
vertex(b.x, b.y, b.z);
vertex(c.x, c.y, c.z);
endShape();
}
1