Emma (hearthand) wrote in algorithms,
Emma
hearthand
algorithms

Computer Graphics - Java

Some backstory for those willing to kindly help me interpret the below java code.

The class is Computer Graphics. What we did in the class was to basically reinvent the wheel; well, the java Graphics2D and Graphics3D API (except... badly) In our projects we managed to master (dun dun DUN!) 3D cubes and viewpoints, hidden surfaces, shaded surfaces and hidden line removal (ta da!) and an insane amount of inheritance. For this question we have to discuss how we might represent a class to represent a cylinder using flat surfaces (where Surface3D inherits from many simple classes: Drawing3D, Line3D to Point3D) to approximate to the figure. 

So far I have established that the paramaters used to specify the cylinder will be: center point, diameter, length and position. An ArrayList data structure will be appropriate for the cylinder and how I will do the class in English. I had a look at the sample answer and I found some java code which I've since been puzzling over because I'm not 100% sure what on earth it's doing.

double cylinder_length = 4; will allow us to specify the length of the cylinder, the length between the two symetrical circles on the axis
double oldx = 1; old x and y co-ordinates that can be saved so we do calculations on previous co-ordinates not the first co-ords.
double oldy = 0;
int n = 32; the set number of points I want around the circle, can be increased to create a smoother surface.

 for (int i = n; i>=0; i--){

double theta = (double)(((double)i/n)*Math.PI *2); 
double x = (double)Math.cos (theta); 
double y = (double)Math.sin (theta); 

Surface3D temp = new Surface3D(); this is us creating a new surface of type surface3D. The "base surface"
cylinder.addGItem(temp);  hierarchy crap that I don't understand but know to do
temp.addPoint(oldx,oldy,0); we are adding the old x, y (0 on the z-axis because the cylinder will start on the xy corords, we aren't rotating it around z) to the circle... making the circle?
temp.addPoint(x,y,0); now adding the next points x and y on the circle calculated above ^
temp.addPoint(x,y,cylinder_length); adding the points x and y identically to the second circle down the z-axis (toward us or away from us depending on the view point)
temp.addPoint(oldx,oldy,cylinder_length); adding the second point on the z-axis that reflects the old xy and y coords
oldx = x; sex new co-ordinates x and y to the old so the new calulations can be done on the new co-ords not the first
oldy = y;

}//for i

Surface3D cylinder_front = new Surface3D(); defining the front of the cylinder that will be able to be seen as a new surface
Surface3D cylinder_back = new Surface3D(); same to the back that should be invisible
cylinder.addGItem(cylinder_front); adding both front and back to cylinder
cylinder.addGItem(cylinder_back);

/*

* which is all fine and well... but then...eh?
*/

for (int i = n; i>=0; i--){
 

double theta = (double)(((double)i/n)*Math.PI *2);
double x = (double)Math.cos (theta);
double y = (double)Math.sin (theta);

System.out.println("theta: " +theta + ", x:"+x+", y:"+y); just printing the values of theta, x and y

cylinder_front.addPoint(x,y,0); I have no idea why we're doing this.... connecting the back co-ords to the front, perhaps? to create the cylinder?
cylinder_back.addPoint(x,y,cylinder_length);

oldx = x;
oldy = y;

}//for i
 

I'm not sure if I'm understanding it correctly or not and I'm quite inexperienced in understanding code by others mainly because I doubt that I'll get it or not.

Thanks in advance for reading!
 
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment