membuat lingkaran dan elips dengan algor

5
KOMPUTER GRAFIK Nama : Zulhamzah Ibrahim NIM : 1205045136 Prodi : PILKOM (A)

Upload: ratnasari

Post on 26-Jan-2016

315 views

Category:

Documents


7 download

DESCRIPTION

grafkom

TRANSCRIPT

Page 1: Membuat Lingkaran Dan Elips Dengan Algor

KOMPUTER GRAFIK

Nama : Zulhamzah IbrahimNIM : 1205045136Prodi : PILKOM (A)

Page 2: Membuat Lingkaran Dan Elips Dengan Algor

Membuat Lingkaran dan Elips dengan Algoritma midpoint di Java

Source code untuk buat bentuk Lingkaran dengan Algoritma Midpoint

public void algoritma_midpoint(Graphics g) { super.paintComponents(g); int r = 50; int x = 0, y = r, p = 1 - r, i = 1; plotCirclePoints(g, x, y, Color.BLACK, 1); while (x < y) { x++; if (p < 0) { p += 2 * x + 1; } else { p += 2 * (x - y) + 1; y--; } plotCirclePoints(g, x, y, Color.BLACK, 1); i++; } }

private void putPixel(Graphics g, int x, int y, Color color, int size) { try { Graphics g2 = (Graphics2D) g; g2.setColor(color.black); g2.fillRect(x, y, size, size); int pixX = Math.abs(x); int pixY = Math.abs(y); pixel[pixX][pixY] = color; } catch (IndexOutOfBoundsException ex) { } catch (Exception ex2) { } }

private void plotCirclePoints(Graphics g, int x, int y, Color color, int size) { int xCenter = 200, yCenter = 250; putPixel(g, xCenter + x, yCenter + y, color, size); putPixel(g, xCenter - x, yCenter + y, color, size); putPixel(g, xCenter + x, yCenter - y, color, size); putPixel(g, xCenter - x, yCenter - y, color, size); putPixel(g, xCenter + y, yCenter + x, color, size); putPixel(g, xCenter - y, yCenter + x, color, size); putPixel(g, xCenter + y, yCenter - x, color, size); putPixel(g, xCenter - y, yCenter - x, color, size);

Page 3: Membuat Lingkaran Dan Elips Dengan Algor

}

Hasil :

Source code untuk buat bentuk Elips dengan Algoritma Midpoint

public void drawEllipseMidpoint(Graphics g) { int rx = 50, ry = 90;

int rx2 = rx * rx, ry2 = ry * ry; int twoRx2 = 2 * rx2, twoRy2 = 2 * ry2; int p, x = 0, y = ry, px = 0, py = twoRx2 * y, i = 0;

plotEllipsePoints(g, x, y, Color.BLACK, 1);

p = (int) Math.round(ry2 - (rx2 * ry) + (0.25 * rx2)); while (px < py) { x++; px += twoRy2; if (p < 0) { p += ry2 + px; } else { y--; py -= twoRx2; p += ry2 + px - py; } plotEllipsePoints(g, x, y, Color.BLACK, 1); i++; }

p = (int) Math.round(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2); while (y > 0) { y--; py -= twoRx2; if (p > 0) {

Page 4: Membuat Lingkaran Dan Elips Dengan Algor

p += rx2 - py; } else { x++; px += twoRy2; p += rx2 - py + px; } plotEllipsePoints(g, x, y, Color.BLACK, 1); i++; } }

private void plotEllipsePoints(Graphics g, int x, int y, Color color, int size) { int xCenter = 200, yCenter = 230; putPixel(g, xCenter + x, yCenter + y, color, size); putPixel(g, xCenter - x, yCenter + y, color, size); putPixel(g, xCenter + x, yCenter - y, color, size); putPixel(g, xCenter - x, yCenter - y, color, size);

}

Hasil :