WEEK 3 : OBJECT ORIENTED CYBER FIBER COMPUTER GRAPHIC
INTRODUCTION :
In week 3, we have to code in a more mature and reusable way. For that we learned object oriented programming. With the help of previous knowledge of 2D shapes, lines, arcs, built in functions, and arrays we must write reusable and managed code for computer graphics. We learned this week how to create classes and implement attributes and methods in it to initiate a class object. We have to implement some objects and make them dynamic to give it an animated look. We have to learn how to call functions.
IDEA FOR COMPUTER GRAPHIC:
Nowadays everything is connected, and communication is quick and reliable. The communication of devices today on networks is a paradigm similar to the manner in which body muscle fibers flow. The muscle tissues are connected to each other through small fibers that share the same structure as mesh cyber network in which communication is through different routes in some visible pattern.
CHALLENGES & PROBLEMS :
The graphic I decided to code was creative and it was very hard for me to understand and implement math into my code. It was challenging for me to provide random movements to the fibers to keep the flow. I used the hit and trial approach to check if the translation of objects is in the right direction which was quite time consuming. Giving angles to the fibers to keep them in flow was challenging which extended with some random movements. It was also very challenging for me to implement math equations in order to keep up the shape of fibers to randomly move between the four corners.
PROCEDURE:
The first step I took is to implement and initiate the built-in functions to create the screen for my graphics. I used setup () and draw () functions for that. Then I started implementing my Fiber class which shows pure use of object oriented programming. I initialized important attributes inside the fiber class which later changed their values during different function calls. I used PVector to give each fiber some specific directions which is randomly generated during function calls. Then, I implemented the constructor of fiber class which takes one parameter which is the size of the fiber. The size of the fiber is decided during object initialization outside the class. After that I began working on some major behaviors of graphics such as moving the fibers and the specific directions. As the fiber moves randomly it is generating more fibers. I made functions for the get random rotations and random addition of fibers during movement. In the movefiber() function I set the major attributes of the class. To display the fibers movement, I implemented displayfiber() function.
//for the movement of Fiber void MoveFibers() { Fiberrotationorignal=radians(random(360)); translatingFiberrotation=MakeFiberRotate(0.1,2.0); Fiberaddition=GetMoreFibers(10000); } //for rotation of Fibers float MakeFiberRotate(float start , float end) { return radians(random(start,end)); } //for addition of more Fibers float GetMoreFibers(int howmuch) { return random(howmuch); }
I used map function in it to transform the values from one range to another. I gave audio-like harmonic movements to additional fibers generated during movement by using noise() function. Subsequently, I angled the fiber vectors which are moving along x and y axes by using cosine and sine functions.
//for displaying Fibers void DisplayFiber() { float noi=map(noise(Fiberaddition),0,1,-20,20); direction.x=(size+noi)*cos(Fiberrotationorignal)*cos(Fiberrotationorignal)*cos(Fiberrotationorignal); direction.y=(size+noi)*sin(Fiberrotationorignal)*sin(Fiberrotationorignal)*sin(Fiberrotationorignal); Fiberrotationorignal+=translatingFiberrotation; Fiberaddition+=0.01; }
I initialized global variables for the number of fibers to generate and initialized a global array for it. These are the initial fibers which were generated during movement. In setup () function I set the color mode to HSB to give hue, saturation and brightness to fibers strokes. I initiated the fibers in 2 halves with different sizes. We can see this in for () loop in setup function.
// Main setup functions to get screen size void setup() { //drawing screen width and height size(640,500); colorMode(HSB,360,100,100); stroke(255); for(int i=0;i<N;i++) { if(i<N/2) { p[i]=new Fiber(180); } else { p[i]=new Fiber(230); } } }
Then, in draw function I made use of the display function to show the movement of fibers along x and y axis. I used translate function to put my graphics on the center of the screen. To give different strokes to different fibers and initializing of fiber in specific direction, I used stroke and line function in nested for loop which runs for N times as long as all fiber objects displayed on the screen. The draw function is looping unlimited times and every time the fibers are being placed in new angles directions.
// For drawing loop of fibers void draw() { colorMode(RGB); background(0); colorMode(HSB,360,100,100); translate(width/2,height/2); for(int i=0;i<N;i++) { p[i].DisplayFiber(); } for(int i=0;i<N;i++) { for(int j=i+1;j<N;j++) { if(p[i].direction.dist(p[j].direction)<120) { stroke(map(p[i].direction.dist(p[j].direction),0,80,50,200),100,100); line(p[i].direction.x,p[i].direction.y,p[j].direction.x,p[j].direction.y); } } } }
FINAL WORK:
Below IS the code work for my computer graphic:
class Fiber { //size of Fiber float size; //orignal rotation of Fiber cluster float Fiberrotationorignal=0; //for Fiber movement rotation float translatingFiberrotation; //direction of fiber PVector direction=new PVector(); //for adding more Fibers in cluster float Fiberaddition; //constructor for Fiber Fiber(float s) { size=s; MoveFibers(); } //for the movement of Fiber void MoveFibers() { Fiberrotationorignal=radians(random(360)); translatingFiberrotation=MakeFiberRotate(0.1,2.0); Fiberaddition=GetMoreFibers(10000); } //for rotation of Fibers float MakeFiberRotate(float start , float end) { return radians(random(start,end)); } //for addition of more Fibers float GetMoreFibers(int howmuch) { return random(howmuch); } //for displaying Fibers void DisplayFiber() { float noi=map(noise(Fiberaddition),0,1,-20,20); direction.x=(size+noi)*cos(Fiberrotationorignal)*cos(Fiberrotationorignal)*cos(Fiberrotationorignal); direction.y=(size+noi)*sin(Fiberrotationorignal)*sin(Fiberrotationorignal)*sin(Fiberrotationorignal); Fiberrotationorignal+=translatingFiberrotation; Fiberaddition+=0.01; } } //Number of Fibers int N=100; //fibers array Fiber p[]=new Fiber[N]; // Main setup functions to get screen size void setup() { //drawing screen width and height size(640,500); colorMode(HSB,360,100,100); stroke(255); for(int i=0;i<N;i++) { if(i<N/2) { p[i]=new Fiber(180); } else { p[i]=new Fiber(230); } } } // For drawing loop of fibers void draw() { colorMode(RGB); background(0); colorMode(HSB,360,100,100); translate(width/2,height/2); for(int i=0;i<N;i++) { p[i].DisplayFiber(); } for(int i=0;i<N;i++) { for(int j=i+1;j<N;j++) { if(p[i].direction.dist(p[j].direction)<120) { stroke(map(p[i].direction.dist(p[j].direction),0,80,50,200),100,100); line(p[i].direction.x,p[i].direction.y,p[j].direction.x,p[j].direction.y); } } } }
CONCLUSION:
This assignment gave me the opportunity to learn object-oriented programming, how to define objects, and different ways to use them to sketch different objects. I learned how to manage the complex code and how to enhance the reusability of code during processing programming. I also learned more knowledge of how to give different angles to objects and how to map one range of values to another range to visualize some useful pattern. I also learned how to use arrays and array indexes to display different objects. I benefitted more in terms of knowledge and the learning of new aspects with this assignment by using arrays inside the for loop.