caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1 | #include "EdgeDemo.h" |
| 2 | #include "EdgeWalker_Test.h" |
| 3 | #include "ShapeOps.h" |
| 4 | #import "SkCanvas.h" |
| 5 | #import "SkPaint.h" |
| 6 | |
| 7 | // Three circles bounce inside a rectangle. The circles describe three, four |
| 8 | // or five points which in turn describe a polygon. The polygon points |
| 9 | // bounce inside the circles. The circles rotate and scale over time. The |
| 10 | // polygons are combined into a single path, simplified, and stroked. |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 11 | static bool drawCircles(SkCanvas* canvas, int step, bool useOld) |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 12 | { |
| 13 | const int circles = 3; |
| 14 | int scales[circles]; |
| 15 | int angles[circles]; |
| 16 | int locs[circles * 2]; |
| 17 | int pts[circles * 2 * 4]; |
| 18 | int c, p; |
| 19 | for (c = 0; c < circles; ++c) { |
| 20 | scales[c] = abs(10 - (step + c * 4) % 21); |
| 21 | angles[c] = (step + c * 6) % 600; |
| 22 | locs[c * 2] = abs(130 - (step + c * 9) % 261); |
| 23 | locs[c * 2 + 1] = abs(170 - (step + c * 11) % 341); |
| 24 | for (p = 0; p < 4; ++p) { |
| 25 | pts[c * 8 + p * 2] = abs(90 - ((step + c * 121 + p * 13) % 190)); |
| 26 | pts[c * 8 + p * 2 + 1] = abs(110 - ((step + c * 223 + p * 17) % 230)); |
| 27 | } |
| 28 | } |
| 29 | SkPath path, out; |
| 30 | for (c = 0; c < circles; ++c) { |
| 31 | for (p = 0; p < 4; ++p) { |
| 32 | SkScalar x = pts[c * 8 + p * 2]; |
| 33 | SkScalar y = pts[c * 8 + p * 2 + 1]; |
| 34 | x *= 3 + scales[c] / 10.0f; |
| 35 | y *= 3 + scales[c] / 10.0f; |
| 36 | SkScalar angle = angles[c] * 3.1415f * 2 / 600; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 37 | SkScalar temp = (SkScalar) (x * cos(angle) - y * sin(angle)); |
| 38 | y = (SkScalar) (x * sin(angle) + y * cos(angle)); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 39 | x = temp; |
| 40 | x += locs[c * 2] * 200 / 130.0f; |
| 41 | y += locs[c * 2 + 1] * 200 / 170.0f; |
| 42 | x += 50; |
| 43 | // y += 200; |
| 44 | if (p == 0) { |
| 45 | path.moveTo(x, y); |
| 46 | } else { |
| 47 | path.lineTo(x, y); |
| 48 | } |
| 49 | } |
| 50 | path.close(); |
| 51 | } |
| 52 | showPath(path, "original:"); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 53 | if (useOld) { |
| 54 | simplify(path, true, out); |
| 55 | } else { |
| 56 | simplifyx(path, out); |
| 57 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 58 | showPath(out, "simplified:"); |
| 59 | SkPaint paint; |
| 60 | paint.setAntiAlias(true); |
| 61 | paint.setStyle(SkPaint::kStroke_Style); |
| 62 | paint.setStrokeWidth(3); |
| 63 | paint.setColor(0x3F007fbF); |
| 64 | canvas->drawPath(path, paint); |
| 65 | paint.setColor(0xFF60FF00); |
| 66 | paint.setStrokeWidth(1); |
| 67 | canvas->drawPath(out, paint); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | static void createStar(SkPath& path, SkScalar innerRadius, SkScalar outerRadius, |
| 72 | SkScalar startAngle, int points, SkPoint center) { |
| 73 | SkScalar angle = startAngle; |
| 74 | for (int index = 0; index < points * 2; ++index) { |
| 75 | SkScalar radius = index & 1 ? outerRadius : innerRadius; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 76 | SkScalar x = (SkScalar) (radius * cos(angle)); |
| 77 | SkScalar y = (SkScalar) (radius * sin(angle)); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 78 | x += center.fX; |
| 79 | y += center.fY; |
| 80 | if (index == 0) { |
| 81 | path.moveTo(x, y); |
| 82 | } else { |
| 83 | path.lineTo(x, y); |
| 84 | } |
| 85 | angle += 3.1415f / points; |
| 86 | } |
| 87 | path.close(); |
| 88 | } |
| 89 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 90 | static bool drawStars(SkCanvas* canvas, int step, bool useOld) |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 91 | { |
| 92 | SkPath path, out; |
| 93 | const int stars = 25; |
| 94 | int pts[stars]; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 95 | // static bool initialize = true; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 96 | int s; |
| 97 | for (s = 0; s < stars; ++s) { |
| 98 | pts[s] = 4 + (s % 7); |
| 99 | } |
| 100 | SkPoint locs[stars]; |
| 101 | SkScalar angles[stars]; |
| 102 | SkScalar innerRadius[stars]; |
| 103 | SkScalar outerRadius[stars]; |
| 104 | const int width = 640; |
| 105 | const int height = 480; |
| 106 | const int margin = 30; |
| 107 | const int minRadius = 120; |
| 108 | const int maxInner = 800; |
| 109 | const int maxOuter = 1153; |
| 110 | for (s = 0; s < stars; ++s) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 111 | int starW = (int) (width - margin * 2 + (SkScalar) s * (stars - s) / stars); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 112 | locs[s].fX = (int) (step * (1.3f * (s + 1) / stars) + s * 121) % (starW * 2); |
| 113 | if (locs[s].fX > starW) { |
| 114 | locs[s].fX = starW * 2 - locs[s].fX; |
| 115 | } |
| 116 | locs[s].fX += margin; |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 117 | int starH = (int) (height - margin * 2 + (SkScalar) s * s / stars); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 118 | locs[s].fY = (int) (step * (1.7f * (s + 1) / stars) + s * 183) % (starH * 2); |
| 119 | if (locs[s].fY > starH) { |
| 120 | locs[s].fY = starH * 2 - locs[s].fY; |
| 121 | } |
| 122 | locs[s].fY += margin; |
| 123 | angles[s] = ((step + s * 47) % (360 * 4)) * 3.1415f / 180 / 4; |
| 124 | innerRadius[s] = (step + s * 30) % (maxInner * 2); |
| 125 | if (innerRadius[s] > maxInner) { |
| 126 | innerRadius[s] = (maxInner * 2) - innerRadius[s]; |
| 127 | } |
| 128 | innerRadius[s] = innerRadius[s] / 4 + minRadius; |
| 129 | outerRadius[s] = (step + s * 70) % (maxOuter * 2); |
| 130 | if (outerRadius[s] > maxOuter) { |
| 131 | outerRadius[s] = (maxOuter * 2) - outerRadius[s]; |
| 132 | } |
| 133 | outerRadius[s] = outerRadius[s] / 4 + minRadius; |
| 134 | createStar(path, innerRadius[s] / 4.0f, outerRadius[s] / 4.0f, |
| 135 | angles[s], pts[s], locs[s]); |
| 136 | } |
| 137 | #define SHOW_PATH 0 |
| 138 | #if SHOW_PATH |
| 139 | showPath(path, "original:"); |
| 140 | #endif |
| 141 | #define TEST_SIMPLIFY 01 |
| 142 | #if TEST_SIMPLIFY |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 143 | if (useOld) { |
| 144 | simplify(path, true, out); |
| 145 | } else { |
| 146 | simplifyx(path, out); |
| 147 | } |
| 148 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 149 | #if SHOW_PATH |
| 150 | showPath(out, "simplified:"); |
| 151 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 152 | SkPaint paint; |
| 153 | paint.setAntiAlias(true); |
| 154 | paint.setStyle(SkPaint::kStroke_Style); |
| 155 | paint.setStrokeWidth(6); |
| 156 | paint.setColor(0x1F003f7f); |
| 157 | canvas->drawPath(path, paint); |
| 158 | paint.setColor(0xFF305F00); |
| 159 | paint.setStrokeWidth(1); |
| 160 | #if TEST_SIMPLIFY |
| 161 | canvas->drawPath(out, paint); |
| 162 | #endif |
| 163 | return true; |
| 164 | } |
| 165 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 166 | static bool (*drawDemos[])(SkCanvas* , int , bool ) = { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 167 | drawStars, |
| 168 | drawCircles |
| 169 | }; |
| 170 | |
| 171 | static size_t drawDemosCount = sizeof(drawDemos) / sizeof(drawDemos[0]); |
| 172 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 173 | static bool (*firstTest)(SkCanvas* , int , bool) = 0; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 174 | |
| 175 | |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 176 | bool DrawEdgeDemo(SkCanvas* canvas, int step, bool useOld) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 177 | size_t index = 0; |
| 178 | if (firstTest) { |
| 179 | while (index < drawDemosCount && drawDemos[index] != firstTest) { |
| 180 | ++index; |
| 181 | } |
| 182 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame^] | 183 | return (*drawDemos[index])(canvas, step, useOld); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 184 | } |