Applet Line between points (x1,y1) and (x2,y2)
A line is one of basic computer elements. While in analytical geometry basic points of a line are included in the line, on a screen a line stacks from end points. Nevertheless, it is relatively simple, its frame is mostly given by a command remark. Composite graphic objects are mostly composed of lines, and for all those it is our tendency to draw a line with the highest effectivity.
A line is usually defined by its end points [x1,y1] and [x2,y2], eventually by one point only [x1,y1], and by a vector of distinction of the co-ordinates [dx, dy] = [x2-x1, y2-y1].
An analytical expression of a line that is not parallel with the axis y, is expressed as
y = m x + q (m, q∈ R)
where m is slope of the line and q is displacement on the axis y. End points of a line determine a line with parameters m and q:
|
(y2-y1)
(x2*y1-x1*y2)
m = ---------- and q = ---------------- (x2-x1) (x2-x1) |
|---|
Raster decomposition of a line is based on sampling with an invariable step according to the axis x, alternatively y. It depends on the inclination of the line, which is represented by the slope m.
Applet Value slope m for different the inclination of the line.
If |m|<1, then the line's inclination to axis x is smaller than 45 grades, therefore the line is patterned according to axis x with a step of 1 pixel. If |m|>1, then it is patterned according to axis y. A line with slope 1 is called diagonal and it is patterned by any axis. The axis according to which it is patterned is called operative (main) axis. The name of second axis is incident axis.
Simple incremental algorithm is well-known also look like DDA (Digital Differential Analyzer) algorithm. The pricnip of this algorithm is based on periodical increase dx for co-ordinate x and also dy for co-ordinate y. Start point is one from end point a line. Because we pattern in raster, increase on directional axis will be (providing, if line be inclined to axis x is directional axis x) right one dot.
Applet Increase for value x and y.
Method is following. In one loop we shall step by step add to co-ordinate on a main axis value 1 and to co-ordinate on incident axis add increase. If axis x is main, value slope equal m.If axis y then value slope equal 1/m. Real number co-ordinates of the displayed objects must be rounded off. DDA algorithm used to calculations new co-ordinate values before dot.
Applet Dwawing a line from left top dot. Please select second end dot. Key 1,2 for zooming.
| The Basic Incremental Algorithm [4].
// drawing a line from starting point x1,y1 to ending point x2,y2 rasterline(int x1, int y1, int x2, int y2)
// if point x1, y1 is rightmost point from
point x2,y2 then replace it
double y = y1;
// increment
// if m < 45 degrees to axis x then
main axis is axis x
public void rastersteepline(int x3, int y3, int x4, int
y4)
// if point x3, y3 is on the right of point x4,y4, change them
double y = y3;
// increment
for (int i = x3; i <= x4; i++)
|
In DDA algorithm we usage real number, which we are then round. In next algorithm we will use only an integral operation.
Applet Bresenham algorithm for a line. Interaction : select end dot, key 1,2 for zooming.
Advance are point at applet, in which is representation
element screen, bunk displayed pixle lines with directional axis x. Advance
from left endpoint
lines (dot and) and own designate, if next dot lines with coordinate
xi+1 own own equal co-ordinate y (tj, yi), alternatively coordinate y at
one biggies (tj. yi+1). Other said whether another dot lines will be dot
B otherwise C. Responsibilities algorithm is designate, which value y own
choose, in order to myself approach right y-coordinate lines.
Bresenham algorithm for a line interactivity: select starting point of a line and moving with mouse transfer ending points.
|
Bresenham algorithm for a line[4]. // draw a line from point x1,y1 into x2,y2
// if point x1, y1 is on the right side of point x2, y2, change them
// test inclination of line
// auxiliary variables
// draw line
public void bresteepline(int x3, int y3, int x4, int y4)
int x = x3, y = y3, sum = x4 - x3,
Dx = 2 * (x4 - x3), Dy = Math.abs(2 * (y4 - y3));
for (int i = 0; i <= x4 -x3;
i++)
|