christian
YaBB Newbies
Offline
Posts: 9
|
angle between 2 vectors (1st solution)
Reply #5 - Jun 4th, 2008, 5:15pm
ok. now heres an answer. basically it is a simple thing to do to get a plane that bisects the angle between two vectors. the tricky part would have been for me to figure out how to get actually the rotation as three values for that plane/matrix so i can use it onwards. so this is it, i guess - get the rotation of a plane..
now heres a solution: given two vectors, you can get the angle bisector by simple addition of their normalized copies. that is the first axis you need. the second one is the cross product, getting a perpendicular vector to the plane those two basic vectors create. you can then just normalize the two new vectors (angle bisector and perpendicular vector) and multiply and offset them, so you end up with that plane bisecting the two vectors (i did not post the vector class again):
import processing.opengl.*;
Vector3D v0; Vector3D v1; Vector3D v2; Vector3D v3; Vector3D v4; Vector3D v5; Vector3D v6;
float dx; float dy; float rx; float ry;
float ax; float ay; float az;
void setup(){
size(800, 600, OPENGL); smooth(); rectMode(CENTER);
float r1 = random(300); float r2 = random(300); float r3 = random(300); v0 = new Vector3D(1,1,1); v1 = new Vector3D(r1, r2, 10); v2 = new Vector3D(r1, 10, r3); Vector3D tv1 = v1.copy(); tv1.normalize(); Vector3D tv2 = v2.copy(); tv2.normalize(); v3 = Vector3D.add(tv1, tv2); v3.normalize(); v3.mult(100); v4 = v1.cross(v2); v4.normalize(); v4.mult(100); }
void draw(){
background(255);
pushMatrix(); translate(width/2, height/2, 0); rotation(); stroke(0); line(0,0,0, v1.x, v1.y, v1.z); line(0,0,0, v2.x, v2.y, v2.z); /* // angle bisector stroke(200,0,0); line(0,0,0, v3.x, v3.y, v3.z); // lotrechte stroke(0,200,0); line(0,0,0, v4.x,v4.y,v4.z); */ // lotrechte und winkelhalbierende geben die beidengeraden // des winkelhalbierenden rechtecks: pushMatrix(); drawRect(v3, v4, true); popMatrix(); popMatrix();
}
void drawRect(Vector3D v1, Vector3D v2, boolean centered){ float offsetx, offsety, offsetz; offsetx = 0; offsety = 0; offsetz = 0; if(centered){ offsetx = -(v1.x+v2.x)/2; offsety = -(v1.y+v2.y)/2; offsetz = -(v1.z+v2.z)/2; } beginShape(); vertex(offsetx,offsety,offsetz); vertex(v1.x+offsetx, v1.y+offsety, v1.z+offsetz); vertex(v2.x+v1.x+offsetx, v2.y+v1.y+offsety, v2.z+v1.z+offsetz); vertex(v2.x+offsetx, v2.y+offsety, v2.z+offsetz); vertex(offsetx,offsety,offsetz); endShape(); }
void rotation(){
dx = (mouseY - height/2) * 0.01; dy = (mouseX - width/2) * 0.01;
rx+=(dx-rx) * 0.1; ry+=(dy-ry) * 0.1;
rotateY(rx); rotateZ(ry); }
|