Skip to content

Instantly share code, notes, and snippets.

@MinSomai
Last active February 22, 2020 17:04
Show Gist options
  • Save MinSomai/5ce625550ffea37538e19f5c4b632657 to your computer and use it in GitHub Desktop.
Save MinSomai/5ce625550ffea37538e19f5c4b632657 to your computer and use it in GitHub Desktop.
BLA algorithm Computer Graphics
package hw;
import java.lang.Math;
import java.applet.Applet;
import java.awt.Graphics;
import java.util.Scanner;
public class BLA13 extends Applet {
int x1, x2, y1, y2;
int a, b;
double Steps;
double dx, dy;
double xINC, yINC;
int x, y;
long p;
public void paint(Graphics g) {
Scanner input = new Scanner(System.in);
System.out.println("Enter x1,y1,x2 and y2 coordinates: ");
x1 = input.nextInt();
y1 = input.nextInt();
x2 = input.nextInt();
y2 = input.nextInt();
if (x2 > x1){
a = 1;
} else {
a = -1;
}
if (y2 > y1){
b = 1;
} else {
b = -1;
}
dx = Math.abs(x2 - x1);
dy = Math.abs(y2 - y1);
x = x1;
y = y1;
if (dx > dy) {
p = Math.round((2 * dy - dx));
for (int i = 0; i < dx; i++) {
g.fillOval(x, y, 5, 5);
if (p <= 0) {
x = x + a;
y = y;
p = (int) (p + 2 * dy);
} else {
x = x + a;
y = y + b;
p = (int) (p + 2 * dy - 2 * dx);
}
}
} else {
p = Math.round((2 * dx - dy));
for (int i = 0; i < dy; i++) {
g.fillOval(x, y, 5, 5);
if (p <= 0) {
x = x;
y = y + b;
p = (int) (p + 2 * dx);
} else {
x = x + a;
y = y + b;
p = (int) (p + 2 * dx - 2 * dy);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment