Created
November 12, 2014 16:31
-
-
Save markogresak/3bd9da600b3b3bc95b93 to your computer and use it in GitHub Desktop.
Dart code for drawing a triangle based on starting coordinates, width and inner angles
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
| void drawLine(x1, y1, x2, y2) { | |
| ctx.beginPath(); | |
| ctx.moveTo(x1, y1); | |
| ctx.lineTo(x2, y2); | |
| ctx.stroke(); | |
| } | |
| double tanDeg(num x) { | |
| return tan(x * PI/180); | |
| } | |
| void drawTriangle(num x1, num y1, num width, num alpha, num beta) { | |
| if(!((alpha >= 0 && alpha < 90 && beta == 90) || (alpha == 90 && beta >= 0 && beta < 90))) | |
| return; | |
| ctx.beginPath(); | |
| ctx.moveTo(x1, y1); | |
| ctx.lineTo(x1 + width, y1); | |
| num angle = alpha < 90 ? alpha : beta; | |
| num y2 = y1 - tanDeg(angle) * width; | |
| num x2 = alpha < 90 ? x1 + width : x1; | |
| ctx.lineTo(x2, y2); | |
| ctx.lineTo(x1, y1); | |
| ctx.fill(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment