Hi @sarath.jasrin,
Here is a small visualization in order to help you to better see how to solve this problem. You have a grid where you can draw points by clicking. When you are finished, you can press enter to toggle the 3d view where you can rotate with your mouse.
-->
// Whether the user finished drawing
boolean finishedDrawing = false;
// The path is a list of points
ArrayList<PVector> path;
void setup() {
size(500, 500, P3D);
// Initialize the path
path = new ArrayList<PVector>();
}
void draw() {
background(0);
// If the user pressed enter
if (finishedDrawing) {
// Move the camera on the z axis (pointing toward the screen)
camera(0, -100, width * 1.5, 0, 0, 0, 0, 1, 0);
// Rotate on the y axis with the mouse
rotateY(map(mouseX, 0, width, 0, PI));
} else {
// Otherwise reset the camera
// Those values were taken from https://processing.org/reference/camera_.html
camera(0, 0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0);
// Display text
fill(255);
textSize(18);
text("Click to add points\nPress ENTER to see 3d", -width/2 + 50, -height/2 + 75);
}
drawAxis(100);
pushMatrix();
// translate to center the grid
translate(-width / 2, -height / 2);
// Draw the grid and the path
drawGrid(50);
drawPath();
popMatrix();
}
void drawAxis(float unit) {
strokeWeight(5);
textSize(40);
// x axis
stroke(255, 0, 0);
line(0, 0, 0, unit, 0, 0);
fill(255, 0, 0);
text("X", unit + 5, 0, 0);
// y axis
stroke(0, 255, 0);
line(0, 0, 0, 0, unit, 0);
fill(0, 255, 0);
text("Y", 0, unit + 5, 0);
if (finishedDrawing) {
// z axis
stroke(0, 0, 255);
line(0, 0, 0, 0, 0, unit);
fill(0, 0, 255);
text("Z", 0, 0, unit + 5);
}
}
// Add a point when clicked (note that we didn't specify a Z coordinate)
void mousePressed() {
if (!finishedDrawing) {
path.add(new PVector(mouseX, mouseY));
}
}
// Toggle the 3d view
void keyPressed() {
if (keyCode == ENTER) {
finishedDrawing = !finishedDrawing;
}
}
// Draw the grid
void drawGrid(float cellSize) {
stroke(255, finishedDrawing? 50 : 255);
strokeWeight(1);
//pushMatrix()
for (int x = 0; x <= width; x += cellSize) {
line(x, 0, x, height);
}
for (int y = 0; y <= height; y += cellSize) {
line(0, y, width, y);
}
}
void drawPath() {
stroke(255, 0, 0);
// Draw lines
strokeWeight(3);
for (int i = 0; i < path.size() - 1; i++) {
line(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y);
}
// Draw points
strokeWeight(12);
for (PVector point : path) {
point(point.x, point.y);
}
}
Again a great tutorial by Daniel Schiffman about P3D (OpenGL in processing) :
And also useful documentation ressources :
EDIT : sorry I just realized that you were asking for p5js but it’s the same concepts and you can easily transpose the code