AgungAlfiansyah

Untitled

Apr 27th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /*
  2. Subrutin membuat lingkaran dengan pusat (0,0) dan jari-jari radius.
  3. Dengan menggunakan persamaan parametrik.
  4. Lingkaran ini digambar penuh
  5. */
  6. void circle1(int xc, int yc, int radius){
  7.   float density=0.5; //ganti nilai density untuk memperoleh gambar yg lebih baik
  8.   int px,py;
  9.   for (float i=0.0; i<45; i=i+density){  //cukup 45 derajat saja
  10.     px=int(radius*cos(radians(i)));      //x
  11.     py=int(radius*sin(radians(i)));      //y
  12.     point(px+xc,py+yc);                  //transformasikan ke yang lain
  13.     point(py+yc,px+xc);
  14.     point(px+xc,-py+yc);
  15.     point(py+yc,-px+xc);
  16.     point(-px+xc,py+yc);
  17.     point(-py+yc,px+xc);
  18.     point(-px+xc,-py+yc);
  19.     point(-py+yc,-px+xc);
  20.    
  21.   }
  22. }
  23.  
  24. /*
  25. Subrutin membuat lingkaran dengan pusat (0,0) dan jari-jari radius.
  26. Dengan menggunakan persamaan quadratik lingkaran.
  27. Lingkaran ini digambar penuh
  28. */
  29. void circle2(int xc, int yc, int radius){
  30.   int py=0;
  31.   for (int px=0; px<=py; px++){  //mulai dari r sampai dengan x=y. pada garis x=y membentuk sudut 45 derajat
  32.     py=int(sqrt(pow(radius,2)-pow(px,2)));   // hitung dengan persamaan lingkaran
  33.     point(px+xc,py+yc);                      //transfo ke yang lain
  34.     point(py+yc,px+xc);
  35.     point(px+xc,-py+yc);
  36.     point(py+yc,-px+xc);
  37.     point(-px+xc,py+yc);
  38.     point(-py+yc,px+xc);
  39.     point(-px+xc,-py+yc);
  40.     point(-py+yc,-px+xc);
  41.  
  42.   }
  43. }
  44.  
  45. /*
  46. Subrutin membuat lingkaran dengan pusat (0,0) dan jari-jari radius.
  47. Dengan menggunakan algoritma Bressenham.
  48. Lingkaran ini digambar tidak penuh, hanya 1/8 saja
  49. */
  50. void circle3(int xc, int yc, int radius){
  51.   int px = 0;
  52.   int py = radius;
  53.   int d1 = 3 - (2 * radius);
  54.  
  55.   boolean rov=true;
  56.  
  57.   while (rov){
  58.     if (px>=py){
  59.        rov=false;}
  60.     if (d1 < 0) {
  61.        d1 = d1 + (4 * px) + 6; }
  62.     else{
  63.       d1 = d1 + 4 * (px-py) + 10; // (1)
  64.       py = py-1;
  65.     }
  66.     point(px+xc,py+yc);   //sama dengan yang sebelumnya
  67.     point(py+yc,px+xc);
  68.     point(px+xc,-py+yc);
  69.     point(py+yc,-px+xc);
  70.     point(-px+xc,py+yc);
  71.     point(-py+yc,px+xc);
  72.     point(-px+xc,-py+yc);
  73.     point(-py+yc,-px+xc);
  74.  
  75.     px++;
  76.   }
  77. }
  78.  
  79. void setup(){
  80.   size(700,700);
  81.   stroke(200,0,0);
  82.   circle1(width/2, height/2, 200);
  83.   stroke(0,0,0);
  84.   circle2(width/2, height/2, 250);
  85.   stroke(0,0,255);
  86.   circle3(width/2, height/2, 300);
  87. }
Add Comment
Please, Sign In to add comment