1.Tentukan titik tengah dan jari-jari
xc , yc , R
2. Tentukan nilai pengubahan posisi
P = I - R;
x = 0;
y = R = 4
3. Perulangan
xc = y
P Selanjutnya = P + (2 * x) + 1 jika P < 0
P Selanjutnya = P + (2 * (x - y)) + 1
4. Translasi dan Pencerminan
(xc + x ,yc + y )
(xc - x ,yc + y )
(xc + x ,yc - y )
(xc - x ,yc - y )
(xc + y ,yc + x )
(xc - y ,yc + x )
(xc + y ,yc - x )
(xc - y ,yc - x )
CONTOH CODING LINGKARAN:
MAIN.JAVA
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lingkaran;
import java.awt.*;
import javax.swing.*;
/**
*
* @author DRIANTAMA
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("algoritma pembuat lingkaran");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(720,480);
frame.setVisible(true);
gambar gbr = new gambar();
gbr.setxc(350);
gbr.setyc(200);
gbr.setr(150);
frame.add(gbr);
}
}
GARIS.JAVA
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lingkaran;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author YOPPY
*/
public class gambar extends JPanel {
private int xc,yc,r;
public int getxc() {
return xc;
}
public int getyc() {
return yc;
}
public int getr() {
return r;
}
public void setxc(int xa) {
xc = xa;
}
public void setyc(int ya) {
yc = ya;
}
public void setr(int jari2) {
r = jari2;
}
public void paint(Graphics g) {
int p = 1-r;
int x = 0;
int y = r;
super.paint(g);
this.setBackground(Color.RED);
g.setColor(Color.cyan);
while(x<=y) {
x++;
if (p<0) {
p+=2*x+1;
} else {
y--;
p+=2*(x-y)+1;
}
g.drawRect(xc+x, yc+y, 1, 1);
g.drawRect(xc-x, yc+y, 1, 1);
g.drawRect(xc+x, yc-y, 1, 1);
g.drawRect(xc-x, yc-y, 1, 1);
g.drawRect(xc+y, yc+x, 1, 1);
g.drawRect(xc-y, yc+x, 1, 1);
g.drawRect(xc+y, yc-x, 1, 1);
g.drawRect(xc-y, yc-x, 1, 1);
}
}
}
Tidak ada komentar:
Posting Komentar