Created
December 16, 2012 08:54
-
-
Save Cellane/4305653 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Protected method that fixes coordinates of a line using Cohen-Sutherland algorithm, | |
| * then calls drawLineBresenham () method to draw the line with corrected coordinates | |
| * | |
| * @param startX x coordinate of start point | |
| * @param startY y coordinate of start point | |
| * @param endX x coordinate of end point | |
| * @param endY y coordinate of end point | |
| * @param color color of painted line | |
| */ | |
| protected void drawLine (int startX, int startY, int endX, int endY, G_Color color) { | |
| int codeStart, codeEnd; | |
| boolean accept; | |
| codeStart = generateCode (startX, startY); | |
| codeEnd = generateCode (endX, endY); | |
| accept = false; | |
| while (true) { | |
| if ((codeStart == 0) && (codeEnd == 0)) { | |
| accept = true; | |
| break; | |
| } else if ((codeStart & codeEnd) != 0) { | |
| break; | |
| } else { | |
| boolean codeWhich; | |
| int workX, workY, codeWork; | |
| // if start code isn't 0 -> true, we're working with start point | |
| // if start code is 0 -> false, we're working with end point | |
| codeWhich = (codeStart != 0); | |
| if (codeWhich) { | |
| codeWork = codeStart; | |
| workX = startX; | |
| workY = startY; | |
| } else { | |
| codeWork = codeEnd; | |
| workX = endX; | |
| workY = endY; | |
| } | |
| if ((codeWork & 1) != 0) { | |
| workY = ((endY - startY) / (endX - startX)) * (Form.leftX - workX) + workY; | |
| workX = Form.leftX; | |
| } else if ((codeWork & 2) != 0) { | |
| workY = ((endY - startY) / (endX - startX)) * (Form.rightX - workX) + workY; | |
| workX = Form.rightX; | |
| } else if ((codeWork & 4) != 0) { | |
| workX = ((endX - startX) / (endY - startX)) * (Form.bottomY - workY) + workX; | |
| workY = Form.bottomY; | |
| } else if ((codeWork & 8) != 0) { | |
| workX = ((endX - startX) / (endY - startX)) * (Form.topY - workY) + workX; | |
| workY = Form.topY; | |
| } | |
| if (codeWork == codeStart) { | |
| startX = workX; | |
| startY = workY; | |
| codeStart = generateCode (startX, startY); | |
| } else { | |
| endX = workX; | |
| endY = workY; | |
| codeEnd = generateCode (endX, endY); | |
| } | |
| } | |
| } | |
| if (accept) { | |
| drawLineBresenham (startX, startY, endX, endY, color); | |
| } | |
| } | |
| /** | |
| * Generates code required for clipping for given point | |
| * 1 = left | |
| * 2 = right | |
| * 4 = bottom | |
| * 8 = top | |
| * | |
| * @param x x coordinate of given point | |
| * @param y y coordinate of given point | |
| * @return calculated code | |
| */ | |
| private int generateCode (int x, int y) { | |
| int code = 0; | |
| if (x < Form.leftX) { | |
| code += 1; | |
| } else if (x > Form.rightX) { | |
| code += 2; | |
| } | |
| if (y > Form.bottomY) { | |
| code += 4; | |
| } else if (y < Form.topY) { | |
| code += 8; | |
| } | |
| return (code); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment