Algoritma Mid Point Pembentuk Garis

Hai sobat ngeposta???
Ayo-ayo nerusin tugas kuliahnya, setelah yang Algoritma Bresenham Pembentuk Garis, kali ini Algoritma Mid Point Pembentuk Garis.
Pembuatannya sama menggunakan netbean 7.3.1 + Plugin OpenGL

Oke langsung disimak aja brother

1.Buka File New Project dan pilih JOGL Application









2. Kopi semua kode dibawah ini dan pastekan dinetbean project anda

/*     */ package org.yourorghere;
/*     */
/*     */ import com.sun.opengl.util.Animator;
/*     */ import java.awt.Frame;
/*     */ import java.awt.event.WindowAdapter;
/*     */ import java.awt.event.WindowEvent;
/*     */ import java.io.PrintStream;
/*     */ import javax.media.opengl.GL;
/*     */ import javax.media.opengl.GLAutoDrawable;
/*     */ import javax.media.opengl.GLCanvas;
/*     */ import javax.media.opengl.GLEventListener;
/*     */ import javax.media.opengl.glu.GLU;
/*     */ import javax.swing.JOptionPane;
/*     */
/*     */ public class elipse
/*     */   implements GLEventListener
/*     */ {
/*     */   int jari_hor;
/*     */   int jari_ver;
/*     */   int xtengah;
/*     */   int ytengah;
/*     */
/*     */   public static void main(String[] args)
/*     */   {
/*  23 */     Frame frame = new Frame("elips");
/*  24 */     GLCanvas canvas = new GLCanvas();
/*     */
/*  26 */     canvas.addGLEventListener(new elipse());
/*  27 */     frame.add(canvas);
/*  28 */     frame.setSize(640, 480);
/*  29 */     Animator animator = new Animator(canvas);
/*  30 */     frame.addWindowListener(new WindowAdapter()
/*     */     {
/*     */       public void windowClosing(WindowEvent e)
/*     */       {
/*  37 */         new Thread(new Runnable()
/*     */         {
/*     */           public void run() {
/*  40 */           
/*  41 */             System.exit(0);
/*     */           }
/*     */         }).start();
/*     */       }
/*     */     });
/*  47 */     frame.setLocationRelativeTo(null);
/*  48 */     frame.setVisible(true);
/*  49 */     animator.start();
/*     */   }
/*     */
/*     */   public void init(GLAutoDrawable drawable)
/*     */   {
/*     */     try {
/*  55 */       String str = JOptionPane.showInputDialog("Masukkan Lebar mendatar ellips");
/*  56 */       int x = Integer.parseInt(str);
/*  57 */       String str1 = JOptionPane.showInputDialog("Masukkan Lebar berdiri ellips");
/*  58 */       int x1 = Integer.parseInt(str1);
/*  59 */       String str2 = JOptionPane.showInputDialog("Masukkan x titik tengah");
/*  60 */       int y = Integer.parseInt(str2);
/*  61 */       String str3 = JOptionPane.showInputDialog("Masukkan y titik tengah");
/*  62 */       int z = Integer.parseInt(str3);
/*  63 */       this.jari_hor = x;
/*  64 */       this.jari_ver = x1;
/*  65 */       this.xtengah = y;
/*  66 */       this.ytengah = z;
/*  67 */       GL gl = drawable.getGL();
/*  68 */       System.err.println("INIT GL IS: " + gl.getClass().getName());
/*     */
/*  71 */       gl.setSwapInterval(1);
/*     */
/*  74 */       gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
/*  75 */       gl.glShadeModel(7425);
/*     */     } catch (Exception e) {
/*  77 */       JOptionPane.showMessageDialog(null, e);
/*  78 */       elipse l = new elipse();
/*  79 */       l.init(drawable);
/*     */     }
/*     */   }
/*     */
/*  83 */   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL();
/*  84 */     GLU glu = new GLU();
/*     */
/*  86 */     if (height <= 0)
/*     */     {
/*  88 */       height = 1;
/*     */     }
/*  90 */     float h = width / height;
/*  91 */     gl.glViewport(0, 0, width, height);
/*  92 */     gl.glMatrixMode(5889);
/*  93 */     gl.glLoadIdentity();
/*  94 */     glu.gluPerspective(11000.0D, h, 1.0D, 20.0D);
/*  95 */     gl.glMatrixMode(5888);
/*  96 */     gl.glLoadIdentity(); }
/*     */
/*     */   public void elips(GLAutoDrawable drawable, int Rx, int Ry, int xCentre, int yCentre) {
/*  99 */     elipse e = new elipse();
/* 100 */     GL gl = drawable.getGL();
/* 101 */     int x = 0; int y = 0;
/*     */
/* 104 */     int Ry2 = Ry * Ry;
/* 105 */     int Rx2 = Rx * Rx;
/* 106 */     int twoRy2 = 2 * Ry2;
/* 107 */     int twoRx2 = 2 * Rx2;
/*     */
/* 109 */     x = 0;
/* 110 */     y = Ry;
/* 111 */     int px = 0;
/* 112 */     int py = twoRx2 * y;
/*     */
/* 114 */     if (2 * Ry * Ry * x < 2 * Rx * Rx * y) {
/* 115 */       int p = (int)Math.round(Ry2 - Rx2 * Ry + 0.25D * Rx2);
/*     */
/* 117 */       e.piksel(drawable, x, y, xCentre, yCentre);
/*     */
/* 119 */       while (px < py) {
/* 120 */         x++;
/* 121 */         px += twoRy2;
/* 122 */         if (p < 0) {
/* 123 */           p += Ry2 + px;
/*     */         } else {
/* 125 */           y--;
/* 126 */           py -= twoRx2;
/* 127 */           p += Ry2 + px - py;
/*     */         }
/* 129 */         e.piksel(drawable, x, y, xCentre, yCentre);
/*     */       }
/*     */     }
/* 132 */     int p = (int)Math.round(Ry2 * (x + 0.5D) * (x + 0.5D) + Rx2 * (y - 1) * (y - 1) - Rx2 * Ry2);
/* 133 */     while (y > 0) {
/* 134 */       y--;
/* 135 */       py -= twoRx2;
/* 136 */       if (p > 0) {
/* 137 */         p += Rx2 - py;
/*     */       } else {
/* 139 */         x++;
/* 140 */         px += twoRy2;
/* 141 */         p += Ry2 + px - py;
/*     */       }
/* 143 */       e.piksel(drawable, x, y, xCentre, yCentre);
/*     */     }
/*     */   }
/*     */
/* 147 */   public void piksel(GLAutoDrawable drawable, int x, int y, int xCentre, int yCentre) { GL gl = drawable.getGL();
/*     */
/* 149 */     gl.glBegin(0);
gl.glColor3f(1.1F, 1.1F, 1.1F);

/* 150 */     gl.glVertex2i(xCentre + x, yCentre + y);
/* 151 */     gl.glVertex2i(xCentre - x, yCentre + y);
/* 152 */     gl.glVertex2i(xCentre + x, yCentre - y);
/* 153 */     gl.glVertex2i(xCentre - x, yCentre - y);
/* 154 */     gl.glEnd();
/* 155 */     gl.glFlush(); }
/*     */
/*     */   public void display(GLAutoDrawable drawable)
/*     */   {
/* 159 */     GL gl = drawable.getGL();
/*     */
/* 162 */     gl.glClear(16640);
/*     */
/* 164 */     gl.glLoadIdentity();
/* 165 */     gl.glPointSize(2.0F);
/*     */
/* 168 */     gl.glTranslatef(-1.5F, 0.0F, -6.0F);
/*     */
/* 170 */     elipse e = new elipse();
/* 171 */     e.elips(drawable, this.jari_hor, this.jari_ver, this.xtengah, this.ytengah);
/*     */   }
/*     */
/*     */   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
/*     */   {
/*     */   }
/*     */ }
hasilnya seperti ini...









yang diatas itu pembentukan elips. Dan kalo yang untuk lingkaran, berikut kodenya...



/*     */ package org.yourorghere;
/*     */
/*     */ import com.sun.opengl.util.Animator;
/*     */ import java.awt.Frame;
/*     */ import java.awt.event.WindowAdapter;
/*     */ import java.awt.event.WindowEvent;
/*     */ import java.io.PrintStream;
/*     */ import javax.media.opengl.GL;
/*     */ import javax.media.opengl.GLAutoDrawable;
/*     */ import javax.media.opengl.GLCanvas;
/*     */ import javax.media.opengl.GLEventListener;
/*     */ import javax.media.opengl.glu.GLU;
/*     */ import javax.swing.JOptionPane;

/*     */
/*     */ public class lingkaran
/*     */   implements GLEventListener
/*     */ {
/*     */   int jari_jari;
/*     */   int xtengah;
/*     */   int ytengah;
/*  22 */  
/*     */
/*     */   public static void main(String[] args) {
/*  25 */     Frame frame = new Frame("Lingkaran");
/*  26 */     GLCanvas canvas = new GLCanvas();
/*     */
/*  28 */     canvas.addGLEventListener(new lingkaran());
/*  29 */     frame.add(canvas);
/*  30 */     frame.setSize(640, 480);
/*  31 */     Animator animator = new Animator(canvas);
/*  32 */     frame.addWindowListener(new WindowAdapter()
/*     */     {
/*     */       public void windowClosing(WindowEvent e)
/*     */       {
/*  39 */         new Thread(new Runnable()
/*     */         {
/*     */           public void run() {
/*  42 */            
/*  43 */             System.exit(0);
/*     */           }
/*     */         }).start();
/*     */       }
/*     */     });
/*  49 */     frame.setLocationRelativeTo(null);
/*  50 */     frame.setVisible(true);
/*  51 */     animator.start();
/*     */   }
/*     */
/*     */   public void midpoint(GLAutoDrawable drawable, int radius, int xcenter, int ycenter) {
/*  55 */     lingkaran c = new lingkaran();
/*  56 */     GLCanvas canvas = new GLCanvas();
/*  57 */     GL gl = drawable.getGL();
/*     */
/*  60 */     int x1 = 0;
/*  61 */     int y1 = radius;
/*  62 */     int p = 1 - radius;
/*  63 */     c.displ(drawable, x1, y1, xcenter, ycenter);
gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
/*     */
/*  65 */     for (int x = 0; x <= y1; x++) {
/*  66 */       x1++;
/*  67 */       if (p < 0) {
/*  68 */         p += 2 * x1 + 1;
/*     */       } else {
/*  70 */         y1 -= 1;
/*  71 */         p += 2 * (x1 - y1) + 1;
/*     */       }
/*     */
/*  74 */       c.displ(drawable, x1, y1, xcenter, ycenter);
/*     */     }
/*     */   }
/*     */
/*     */   public void init(GLAutoDrawable drawable)
/*     */   {
/*     */     try {
/*  81 */       GL gl = drawable.getGL();
/*     */
/*  83 */       String str = JOptionPane.showInputDialog("Masukkan diameter Lingkaran");
/*  84 */       int x = Integer.parseInt(str);
/*  85 */       String str2 = JOptionPane.showInputDialog("Masukkan x titik tengah");
/*  86 */       int y = Integer.parseInt(str2);
/*  87 */       String str3 = JOptionPane.showInputDialog("Masukkan y titik tengah");
/*  88 */       int z = Integer.parseInt(str3);
/*  89 */       this.jari_jari = x;
/*  90 */       this.xtengah = y;
/*  91 */       this.ytengah = z;
/*     */
/*  94 */       System.err.println("INIT GL IS: " + gl.getClass().getName());
/*     */
/*  97 */       gl.setSwapInterval(0);
/*     */
/* 100 */       gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
/* 101 */       gl.glShadeModel(7425);
/*     */     } catch (Exception e) {
/* 103 */       JOptionPane.showMessageDialog(null, e);
/* 104 */       lingkaran l = new lingkaran();
/* 105 */       l.init(drawable);
/*     */     }
/*     */   }
/*     */
/* 109 */   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL();
/* 110 */     GLU glu = new GLU();
/*     */
/* 112 */     if (height <= 0)
/*     */     {
/* 114 */       height = 1;
/*     */     }
/* 116 */     float h = width / height;
/* 117 */     gl.glViewport(0, 0, width, height);
/* 118 */     gl.glMatrixMode(5889);
/* 119 */     gl.glLoadIdentity();
/* 120 */     glu.gluPerspective(11000.0D, h, 1.0D, 20.0D);
/* 121 */     gl.glMatrixMode(5888);
/* 122 */     gl.glLoadIdentity(); }
/*     */
/*     */   public void display(GLAutoDrawable drawable)
/*     */   {
/* 126 */     GL gl = drawable.getGL();
/* 127 */     gl.glClear(16640);
/* 128 */     gl.glLoadIdentity();
/*     */
/* 130 */     gl.glTranslatef(0.0F, 0.0F, -20.0F);
/*     */
/* 132 */     gl.glPointSize(1.5F);
/* 133 */     lingkaran s = new lingkaran();
/* 134 */     s.midpoint(drawable, this.jari_jari, this.xtengah, this.ytengah);
/* 135 */     gl.glEnd();
/* 136 */     gl.glFlush();
/*     */   }
/*     */
/*     */   public void displ(GLAutoDrawable drawable, int x1, int y1, int xcenter, int ycenter) {
/* 140 */     GL gl = drawable.getGL();
/*     */
/* 142 */     gl.glBegin(0);
/* 143 */    gl.glColor3f(1.0F, 1.0F, 1.0F);
/* 144 */     gl.glVertex2i(xcenter + x1, ycenter + y1);
/* 145 */     gl.glVertex2i(xcenter - x1, ycenter + y1);
/* 146 */     gl.glVertex2i(xcenter + x1, ycenter - y1);
/* 147 */     gl.glVertex2i(xcenter - x1, ycenter - y1);
/* 148 */     gl.glVertex2i(xcenter + y1, ycenter + x1);
/* 149 */     gl.glVertex2i(xcenter - y1, ycenter + x1);
/* 150 */     gl.glVertex2i(xcenter + y1, ycenter - x1);
/* 151 */     gl.glVertex2i(xcenter - y1, ycenter - x1);
/*     */
/* 153 */     gl.glEnd();
/* 154 */     gl.glFlush();
/*     */   }
/*     */
/*     */   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
/*     */   {
/*     */   }
/*     */ }


dan untuk penampakannya seperti ini ...






Bagi yang ga mau repot-repot, aku sediakan projectku
Silahakan download ===========> disini
Cara bukanya, klik File Open Project, kemudian kalian browse project yang telah kalian download


Demikian artikelku tentang Algoritma Mid Point Pembentuk Garis. Semoga bermanfaat untuk kalian semua. Salam Quincy ^^
Previous
Next Post »

Tata Cara Berkomentar yang Baik dan Benar :

1. Gunakan Bahasa yang Sopan.
2. Biasakanlah berkomentar sebelum meninggalkan.
3. Usahakan jangan menggunakan anonymous
4. Saya sangat berterima kasih atas komentar yang kalian berikan
5. Admin selalu berusaha melakukan yang terbaik untuk para pengunjung ConversionConversion EmoticonEmoticon

Thanks for your comment