我想制作一个程序,在Processing中绘制一个三轴坐标系,并将点坐标A(x,y,z)作为输入,并将其显示在三轴坐标系中,任何人都可以在这里为我提供一个代码我可以从?
开始答案 0 :(得分:2)
您可以使用line()函数轻松绘制轴并传递两对x,y,z坐标("来自""到"点3D线)。
绘制3条线并用颜色(例如X,Y,Z为R,G,B)着色每个轴应该这样做:
void drawAxes(float size){
//X - red
stroke(192,0,0);
line(0,0,0,size,0,0);
//Y - green
stroke(0,192,0);
line(0,0,0,0,size,0);
//Z - blue
stroke(0,0,192);
line(0,0,0,0,0,size);
}
如果您打算使用多个坐标系,那么值得阅读2D transformations tutorial。在使用pushMatrix() / popMatrix()调用隔离和嵌套坐标系方面,相同的概念也适用于3D:
PVector a = new PVector(100,50,20);
void setup(){
size(400,400,P3D);
strokeWeight(3);
}
void draw(){
background(255);
//draw original coordinate system
drawAxes(100);
//draw from centre and rotate with mouse
translate(width * 0.5, height * 0.5, 0);
rotateX(map(mouseY,0,height,-PI,PI));
rotateY(map(mouseX,0,width,PI,-PI));
//draw centred coordinate system
drawAxes(100);
//isolate coordinate system for A point
pushMatrix();
translate(a.x,a.y,a.z);
//draw translated A point
drawAxes(50);
popMatrix();
}
void drawAxes(float size){
//X - red
stroke(192,0,0);
line(0,0,0,size,0,0);
//Y - green
stroke(0,192,0);
line(0,0,0,0,size,0);
//Z - blue
stroke(0,0,192);
line(0,0,0,0,0,size);
}
您可以运行p5.js preview以下:
<iframe width="400" height="400" src="https://alpha.editor.p5js.org/embed/HkQoQTAvl" style="border:none;"></iframe>
&#13;
答案 1 :(得分:0)