/* Line function * * Revised line function that will draw full lines * in any slope at any angle. */ void Line(int X1, int Y1, int X2, int Y2, unsigned char C) { float currX, currY; /* Current pixel to be plotted */ float deltaX, deltaY, slope; /* 2 deltas and slope */ int tempVal; /* Holds temporary values */ deltaX = X2 - X1; /* Get value for delta X */ deltaY = Y2 - Y1; /* Get value for delta Y */ if (deltaX < 0) deltaX = -deltaX; /* Need absolute */ if (deltaY < 0) deltaY = -deltaY; /* values only. */ if (deltaY > deltaX) { /* |slope| > 1 */ if (Y2 < Y1) { /* Swap coordinates, Y1 must be < Y2 */ tempVal = Y2; Y2 = Y1; Y1 = tempVal; tempVal = X2; X2 = X1; X1 = tempVal; } /* Compute deltas and slope */ deltaX = X2 - X1; deltaY = Y2 - Y1; /* Slope off the vertical axis */ slope = deltaX / deltaY; /* Plot each pixel starting at (X1, Y1) */ currX = X1; for (currY = Y1; currY <= Y2; currY++) { SetPixel((int)currX, (int)currY, C); currX += slope; /* increment X by the slope */ } return; } else if ((deltaY || deltaX) != 0) { /* |slope| < 1 */ if (X2 < X1) { /* Swap coordinates, X1 must be < X2 */ tempVal = X2; X2 = X1; X1 = tempVal; tempVal = Y2; Y2 = Y1; Y1 = tempVal; } /* Compute deltas and slope */ deltaX = X2 - X1; deltaY = Y2 - Y1; /* Slope off the horizontal axis */ slope = deltaY / deltaX; /* Plot each pixel starting at (X1, Y1) */ currY = Y1; for (currX = X1; currX <= X2; currX++) { SetPixel((int)currX, (int)currY, C); currY += slope; /* increment Y by the slope */ } return; } else /* Set a single pixel because Y1 = Y2 and X1 = X2 */ SetPixel(X1, Y1, C); }