We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to write code that will take 2 points and draw a long thin shape between them.
I have successfully done this in a 2d space. The process involves:
Here's the code for that:
float x1 = -200;
float y1 = 0;
float x2 = 200;
float y2 = 200;
float plane_width = 20;
void setup() {
size(500,500);
}
void draw() {
translate(width/2, height/2);
background(255);
draw_plane_along_line(x1,y1,x2,y2, plane_width);
line(x1,y1,x2,y2);
}
float get_angle_of_line(float x1, float y1, float x2, float y2) {
float x_delta = x2 - x1;
float y_delta = y2 - y1;
return atan2(y_delta, x_delta);
}
void draw_plane_along_line(float x1, float y1, float x2, float y2, float w) {
float angle = get_angle_of_line(x1,y1,x2,y2);
float angle_left = angle - HALF_PI;
float angle_right = angle + HALF_PI;
float left_corner_x = w/2 * cos(angle_left) + x1;
float left_corner_y = w/2 * sin(angle_left) + y1;
float right_corner_x = w/2 * cos(angle_right) + x1;
float right_corner_y = w/2 * sin(angle_right) + y1;
float end_left_corner_x = w/2 * cos(angle_left) + x2;
float end_left_corner_y = w/2 * sin(angle_left) + y2;
float end_right_corner_x = w/2 * cos(angle_right) + x2;
float end_right_corner_y = w/2 * sin(angle_right) + y2;
beginShape();
vertex(left_corner_x, left_corner_y);
vertex(end_left_corner_x, end_left_corner_y);
vertex(end_right_corner_x, end_right_corner_y);
vertex(right_corner_x, right_corner_y);
endShape(CLOSE);
}
Now I'm trying to do the same thing in a 3d space.
It seems that the same theory should apply - except now there should be 2 angles to calculate: the angle from the x-axis, and then the angle from the y-axis. The theory seems simple, but I'm not sure of the maths.
Can anyone help? I'm happy to scrap this code completely if there's a much simpler way! Or indeed if there is a library that will take care of this for me.
Answers
the problem is that there are an infinite number of planes between two points in 3d space. which one are you using?
(this is why camera() takes 9 parameters, a start, an end and an 'up' vector)
(btw there is something similar to the 2d version in the shaders tutorial, https://processing.org/tutorials/pshader/ - Code listing 10.4)
hi @koogs,
If you're asking which way I want to be "up", I'm happy to assume that it's the positive end of the y-axis... if that makes sense. I'm not sure if that answers your question though.
When I use the word "plane" in the above code, what I really meant was "2d shape" - I may be using the wrong terminology.
If you are interested in just using
Java
mode then you could use the Shapes3D library like thisOr look at drawLine here
https://processing.org/discourse/beta/num_1263658331.html
There are two problems with using line(x0,y0,z0,x1,y1.z1) to draw lines in OpenGL.
1) you will not get the perspective effect
2) when combined with surfaces (triangles and quads) depth sorting the lines/surfaces correctly is not very efficient.
See this discussion
@quark - that library is great, thank you!
Thanks for the suggestion @Chrisir - line() was my original solution, but as @quark points out, there is no perspective effect.
??
you got me wrong
there is a function I posted the Link to which essentially is a thin long rect or box
name of the function is drawLine iirc
Sorry my mistake
;-)
no problem
just strokeColour
is in fact fillColor
;-)
Ah, apologies for the misunderstanding @Chrisir - I might try out that function at some point, thanks for the link!
Sure