**Drawing a line** definitely belongs into basics of **computer graphics**. There are many algorithms for drawing a line. Some are more effective (**Bresenham's**), some are less (**Trivial, DDA**). Although you don't actually need to implement these algorithms yourself (you can you **java.awt.Graphics**), it's good to know that methods in this package actually do. I'll show you a java code for these 3 algorithms. Implementation in Java is very similar to C, so C# and other C's lover might look at it as well.

You can try my Functional java app that uses all these three algorithms below + some more additions :)

Sorry that the code uses some variables with Czech names and the same applies for the app, but it was just a school project.

## Bresenham's line algorithm

Probably the **most effective algorithm to draw a line** by. It originates from the DD algorithm, but this variations uses only integers (int) - that's a huge advantage for Bresenham.

The code below is a code variation that covers all the situations that can occur during **drawing a line** - drawing properly in **all halfquadrants** according to the **angle**. It also draws perfectly when you start on the right side and end on the left or when the line is just 1 pixel long.

```
// Bresenham's algorithm
if ((x1 == x2) && (y1 == y2)) {
drawPixel(x1, y1, barva);
} else {
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int rozdil = dx - dy;
int posun_x, posun_y;
if (x1 < x2) posun_x = 1; else posun_x = -1;
if (y1 < y2) posun_y = 1; else posun_y = -1;
while ((x1 != x2) || (y1 != y2)) {
int p = 2 * rozdil;
if (p > -dy) {
rozdil = rozdil - dy;
x1 = x1 + posun_x;
}
if (p < dx) {
rozdil = rozdil + dx;
y1 = y1 + posun_y;
}
drawPixel(x1, y1, barva);
}
}
```

## DDA line algorithm

**A pattern for Bresenham's algorithm. **The main difference is the date type of numbers - DDA uses real numbers, meaning** float or double**. But it seems to be a bit easier to understand.

The code below is a code variation that covers all the situations that can occur during **drawing a line** - drawing properly in **all halfquadrants** according to the **angle**. It also draws perfectly when you start on the right side and end on the left or when the line is just 1 pixel long.

```
// DDA algorithm
double dx = x2-x1;
double dy = y2-y1;
if (Math.abs(y2 - y1) <= Math.abs(x2 - x1)) {
if ((x1 == x2) && (y1 == y2)) {
drawPixel(x1, y1, barva);
} else {
if (x2 < x1) {
int tmp = x2;
x2 = x1;
x1 = tmp;
tmp = y2;
y2 = y1;
y1 = tmp;
}
double k = (double)dy/dx;
int cele_y;
double y = (double)y1;
for (int x = x1 ; x <= x2 ; x++) {
cele_y = (int)Math.round(y);
drawPixel(x, cele_y, barva);
y += k;
}
}
} else {
if (y2 < y1) {
int tmp = x2;
x2 = x1;
x1 = tmp;
tmp = y2;
y2 = y1;
y1 = tmp;
}
double k = (double)dx/dy;
int cele_x;
double x = (double)x1;
for (int y = y1; y <= y2; y++) {
cele_x = (int)Math.round(x);
drawPixel(cele_x, y, barva);
x += k;
}
}
```

## Trivial line algorithm

**The simplest algorithm for line drawing.** Its logic is pretty straightforward - exactly as you would do it in math (analytic geometry, you remember, huh?). All these algorithms are mathematical, actually, but in this case, it's extremely clear.

The code below is a code variation that covers all the situations that can occur during **drawing a line** - drawing properly in **all halfquadrants** according to the **angle**. It also draws perfectly when you start on the right side and end on the left or when the line is just 1 pixel long.

```
// Trivial algorithm
double dx = x2 - x1;
double dy = y2 - y1;
if (Math.abs(y2 - y1) >= Math.abs(x2 - x1)) {
if ((x1 == x2) && (y1 == y2)) {
drawPixel(x1, y1, barva);
} else {
if (y2 < y1) {
int tmp = x2;
x2 = x1;
x1 = tmp;
tmp = y2;
y2 = y1;
y1 = tmp;
}
double k = dx / (double) dy;
double q = x1 - k * y1;
for (int y = y1; y < y2; y++) {
int x = (int) (k * y + q);
drawPixel(x, y, barva);
}
}
} else {
if (x2 < x1) {
int tmp = x2;
x2 = x1;
x1 = tmp;
tmp = y2;
y2 = y1;
y1 = tmp;
}
double k = dy / (double) dx;
double q = y1 - k * x1;
for (int x = x1; x < x2; x++) {
int y = (int) (k * x + q);
drawPixel(x, y, barva);
}
}
```