How to resolve the algorithm Draw a sphere step by step in the Processing programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Draw a sphere step by step in the Processing programming language

Table of Contents

Problem Statement

Draw a sphere. The sphere can be represented graphically, or in ASCII art, depending on the language capabilities. Either static or rotational projection is acceptable for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Draw a sphere step by step in the Processing programming language

Source code in the processing programming language

void setup() {
  size(500, 500, P3D);
}
void draw() {
  background(192);
  translate(width/2, height/2);
  // optional color and lighting style
  stroke(200);
  fill(255);
  lights();
  // draw sphere
  sphere(200);
}


float rotX, rotY;

PVector[][] sphere;
int detail = 50;

void setup() {
  size(600, 600, P3D);
  background(50, 50, 200);
  fill(255, 0, 200);
  stroke(0, 40);
  sphere = new PVector[detail+1][detail+1];
}

void draw() {
  pointLight(255, 255, 255, -width, -height, 2*height);
  translate(width/2, height/2);
  rotateX(rotX);
  rotateY(rotY);
  float r = 200;
  for (int i = 0; i <= detail; i++) {
    float rows = map(i, 0, detail, 0, PI);
    for (int j = 0; j <= detail; j++) {
      float columns = map(j, 0, detail, 0, TWO_PI);
      float x = r * sin(rows) * cos(columns);
      float y = r * sin(rows) * sin(columns);
      float z = r * cos(rows);
      sphere[i][j] = new PVector(x, y, z);
    }
  }
  for (int i = 0; i < detail; i++) {
    beginShape(TRIANGLE_STRIP);
    for (int j = 0; j <= detail; j++) {
      PVector v1 = sphere[i][j];
      vertex(v1.x, v1.y, v1.z);
      PVector v2 = sphere[i+1][j];
      vertex(v2.x, v2.y, v2.z);
    }
    endShape();
  }
}

void mouseDragged() {
  rotY -= (mouseX - pmouseX) * 0.01;
  rotX -= (mouseY - pmouseY) * 0.01;
}


  

You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Julia programming language
You may also check:How to resolve the algorithm Chernick's Carmichael numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the C programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Forth programming language
You may also check:How to resolve the algorithm Fractran step by step in the BQN programming language