Ini adalah Source code untuk buat bentuk Garis dengan Algoritma DDA
public void drawline_DDA(Graphics g) {
super.paintComponents(g);
int langkah;
int xInc, yInc;
int x0 = 120,
y0 = 230;
int x1 = 270, y1 = 230;
int x = x0, y = y0;
int dx = x1 - x0,
dy = y1 - y0;
if (Math.abs(dx) > Math.abs(dy)) {
langkah = Math.abs(dx);
} else {
langkah = Math.abs(dy);
}
xInc = dx / langkah;
yInc = dy / langkah;
for (x = x0; x < x1; x++) {
x += xInc;
y += yInc;
putPixel(g, Math.round(x), Math.round(y), Color.black, 6);
}
}
Outputnya kaya gini nih
Ini adalah Source code untuk buat bentuk Garis dengan Algoritma Bresenheim
void drawLine_bresenham(Graphics g) {
super.paintComponents(g);
int xa = 120, xb = 270, ya = 230, yb = 230, p, x, y, xend;
int dx = Math.abs(xb - xa);
int dy = Math.abs(yb - ya);
p = (2 * dy) - dx;
if (xa > xb) {
x = xb;
y = yb;
xend = xa;
} else {
x = xa;
y = ya;
xend = xb;
}
putPixel(g, Math.round(x), Math.round(y), Color.black, 1);
for (x = xa; x < xend; x++) {
if (p < 0) {
p = p + (2 * dy);
} else {
y = y + 1;
p = p + (2 * (dy - dx));
}
putPixel(g, Math.round(x), Math.round(y), Color.black, 1);
}
}
Output Nya kaya gini nih
Ini adalah 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);
}
Output Nya kaya gini nih
Ini adalah 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) {
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);
}
Output Nya kaya gini nih
Ini adalah Source code untuk buat bentuk Persegi dengan Algoritma DDA
public void kotak_dda(Graphics g) {
//variable
int a0, b0, a1, b1, dx, dy, step, tx, ty;
int x, y, x_tambah, y_tambah;
//inisial translasi
tx = 80;
ty = 70;
//koordinat titik
a0 = 70;
b0 = 70;
a1 = 200;
b1 = 200;
//translasi
int x0 = a0 + tx;
int x1 = a1 + tx;
int y0 = b0 + ty;
int y1 = b1 + ty;
dx = x1 - x0;
dy = y1 - y0;
x = x0;
y = y0;
//faktor pembagi
if (dx > dy) {
step = dx;
} else {
step = dy;
}
x_tambah = dx / step;
y_tambah = dy / step;
//menggambar kotak
for (int k = 0; k < step; k++) {
x += x_tambah;
y += y_tambah;
putPixel(g, Math.round(x), Math.round(y0), Color.black, 1);
putPixel(g, Math.round(x), Math.round(y1), Color.black, 1);
putPixel(g, Math.round(x0), Math.round(y), Color.black, 1);
putPixel(g, Math.round(x1), Math.round(y), Color.black, 1);
}
}
Output Nya kaya gini nih
Minggu, 24 Juni 2012
Langganan:
Posting Komentar (Atom)





1 komentar:
mbak ini buatnya dimana ya ??
Posting Komentar