任意の点を通過する曲線を描く場合、cubicCurveToを使うとなめらかでシンプルなカーブを描ける。
曲線補間 – wonderfl build flash online
たとえば、マウスの軌跡に合わせた線が欲しいとする。
mouseMoveなり、enterFrameなりで座標を取得することになるわけだけれども、マウスの移動が早い場合座標同士が離れてしまうので、カクカクな線になってしまう。
これをスムーズな曲線にしたい場合、何らかの方法で補完してあげる必要がある。
今までのcurveToの場合、与える座標に対してぴったり一致させる曲線を描くのが難しかった。
cubicCurveToだと、簡単ってのがここで示そうとしていること。
小さな丸が、ランダムに生成した座標。
これらを薄いグレーの直線で結んでいる。
赤や青のラインはcurveToで曲線を作っている。
青のラインは綺麗なんだけど、実際は丸を通過していない。マウスの軌跡くらいならほとんどの場合これで十分だけど。
赤のラインは、小さな丸をちゃんと通過しているけど、なめらかなカーブが作れない場合がある。工夫すればカーブ自体はなめらかになるかもしれないけど、複雑なラインになってしまうのは避けられない。
緑のラインはcubicCurveToを使ったもの。ちゃんと丸を通過している。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
package { import com.bit101.components.CheckBox; import com.bit101.components.Label; import com.bit101.components.Slider; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; /** * ... * @author umhr */ public class WonderflMain3 extends Sprite { [SWF(width = 465, height = 465, backgroundColor = 0xFFFFFF, frameRate = 30)] private var _pointList:Array/*Point*/ = []; private var _checkBoxList:Array/*CheckBox*/ = []; private var _circumscribedCurve:CircumscribedCurve; private var _inscribedCurve:InscribedCurve; private var _cubicCurve:CubicBezierCurve; private var _line:Line; private var _segmentLabel:Label; private var _randomLabel:Label; private var _tangentialLineLabel:Label; private var _segment:int = 5; private var _random:Number = 200; private var _tangentialLineRetio:Number = 0.33; public function WonderflMain3() { init(); } private function init():void { _circumscribedCurve = new CircumscribedCurve(graphics); _inscribedCurve = new InscribedCurve(graphics); _cubicCurve = new CubicBezierCurve(graphics); _line = new Line(graphics); var segmentSlider:Slider = new Slider("horizontal", this, 230, 20, slider_change); segmentSlider.width = 220; segmentSlider.setSliderParams(4, 30, _segment); var randomSlider:Slider = new Slider("horizontal", this, 230, 55, randomSlider_change); randomSlider.width = 220; randomSlider.setSliderParams(0, 400, _random); var tangentialSlider:Slider = new Slider("horizontal", this, 230, 90, tangentialSlider_change); tangentialSlider.width = 220; tangentialSlider.setSliderParams(0, 2, _tangentialLineRetio); _segmentLabel = new Label(this, 230, 30, "Segments : " + _segment); _randomLabel = new Label(this, 230, 65, "Random : " + _random); _tangentialLineLabel = new Label(this, 230, 100, "TangentialLineRetio(for CubicBezierCurve) : " + _tangentialLineRetio); _checkBoxList[0] = new CheckBox(this, 25, 355, "Line", checkBox_click); _checkBoxList[0].selected = true; (_checkBoxList[0].getChildAt(2) as Label).textField.textColor = 0x888888; _checkBoxList[1] = new CheckBox(this, 25, 380, "QuadraticBezierCurve(Circumscribed)", checkBox_click); _checkBoxList[1].selected = true; (_checkBoxList[1].getChildAt(2) as Label).textField.textColor = 0xFF0000; _checkBoxList[2] = new CheckBox(this, 25, 405, "QuadraticBezierCurve(Inscribed)", checkBox_click); _checkBoxList[2].selected = true; (_checkBoxList[2].getChildAt(2) as Label).textField.textColor = 0x0000FF; _checkBoxList[3] = new CheckBox(this, 25, 430, "CubicBezierCurve", checkBox_click); _checkBoxList[3].selected = true; (_checkBoxList[3].getChildAt(2) as Label).textField.textColor = 0x00FF00; click(null); stage.addEventListener(MouseEvent.CLICK, click); //addEventListener(Event.ENTER_FRAME, enterFrame); } private function checkBox_click(e:Event):void { var check:CheckBox = e.target as CheckBox; } private function tangentialSlider_change(e:Event):void { _tangentialLineRetio = Number(e.target.value); _tangentialLineLabel.text = "TangentialLineRetio(for CubicBezierCurve) : " + _tangentialLineRetio; click(null); } private function randomSlider_change(e:Event):void { _random = int(e.target.value); _randomLabel.text = "Random : " + _random; click(null); } private function slider_change(e:Event):void { _segment = int(e.target.value); _segmentLabel.text = "Segments : " + _segment; click(null); } private function enterFrame(e:Event):void { _pointList.length = Math.min(_pointList.length, 50); graphics.clear(); _pointList.unshift(new Point(mouseX, mouseY)); draw(); } private function click(e:MouseEvent):void { _pointList.length = 0; graphics.clear(); var n:int = _segment; for (var i:int = 0; i < n; i++) { var pt:Point = new Point(); pt.x = (i / (n - 1)) * (449 - _random) + _random * Math.random() + 16 * 0.5; pt.y = (i / (n - 1)) * (449 - _random) + _random * Math.random() + 16 * 0.5; _pointList.unshift(pt); } draw(); } private function draw():void { if(_checkBoxList[0].selected){ _line.draw(_pointList); } if(_checkBoxList[1].selected){ _circumscribedCurve.draw(_pointList); } if(_checkBoxList[2].selected){ _inscribedCurve.draw(_pointList); } if(_checkBoxList[3].selected){ _cubicCurve.draw(_pointList, _tangentialLineRetio); } } } } import flash.display.Graphics; import flash.geom.Point; class Line { private var _graphics:Graphics; public function Line(graphics:Graphics) { _graphics = graphics; } public function draw(pointList:Array/*Point*/):void { var i:int; var n:int = pointList.length; if (n > 1) { var m:int = n; _graphics.lineStyle(0, 0x888888, 0.3); _graphics.moveTo(pointList[0].x, pointList[0].y); for (i = 0; i < m; i++) { _graphics.lineTo(pointList[i].x, pointList[i].y); } } _graphics.lineStyle(0, 0x000000, 0.5); for (i = 0; i < n; i++) { _graphics.drawCircle(pointList[i].x, pointList[i].y, 2); } } } // 2次ベジェ曲線(内接) class InscribedCurve { private var _graphics:Graphics; public function InscribedCurve(graphics:Graphics) { _graphics = graphics; } public function draw(pointList:Array/*Point*/):void { var n:int = pointList.length; if (n > 2) { var m:int = n - 2; for (var i:int = 0; i < m; i++) { drawCurve(pointList[i], pointList[i + 1], pointList[i + 2], i == 0, i == (m - 1)); } } } private function drawCurve(pt0:Point, pt1:Point, pt2:Point, isIn:Boolean, isOut:Boolean):void { var p0:Point = new Point((pt1.x + pt0.x) * 0.5, (pt1.y + pt0.y) * 0.5); var p1:Point = new Point((pt1.x + pt2.x) * 0.5, (pt1.y + pt2.y) * 0.5); _graphics.lineStyle(0, 0x0000FF, 0.5); if (isIn) { _graphics.moveTo(pt0.x, pt0.y); _graphics.lineTo(p0.x, p0.y); } _graphics.moveTo(p0.x, p0.y); _graphics.curveTo(pt1.x, pt1.y, p1.x, p1.y); if (isOut) { _graphics.lineTo(pt2.x, pt2.y); } } } // 3次ベジェ曲線 class CubicBezierCurve { private var _graphics:Graphics; private var _tangentialLineLength:Number; public function CubicBezierCurve(graphics:Graphics) { _graphics = graphics; } public function draw(pointList:Array/*Point*/, tangentialLineLength:Number):void { _tangentialLineLength = tangentialLineLength; var n:int = pointList.length; if (n > 3) { var m:int = n - 3; for (var i:int = 0; i < m; i++) { drawLine(pointList[i], pointList[i + 1], pointList[i + 2], pointList[i + 3], i == 0, i == (m - 1)); } } } private function drawLine(pt0:Point, pt1:Point, pt2:Point, pt3:Point, isIn:Boolean, isOut:Boolean):void { var p1:Point = Calc.tangentialLine(pt0, pt1, pt2); var p2:Point = Calc.tangentialLine(pt3, pt2, pt1); var clossPoint:Point = Calc.getClossPoint(pt1, pt2, pt1.add(p1), pt2.add(p2)); var cpt1:Point = clossPoint.subtract(pt1); var cpt2:Point = clossPoint.subtract(pt2); // 接線 var length:Number = pt1.subtract(pt2).length * _tangentialLineLength; p1.normalize(length); p2.normalize(length); _graphics.lineStyle(0, 0x66FF66, 0.3); _graphics.moveTo(pt1.x, pt1.y); _graphics.lineTo(pt1.x + p1.x, pt1.y + p1.y); _graphics.lineStyle(0, 0x66FF66, 0.3); _graphics.moveTo(pt2.x, pt2.y); _graphics.lineTo(pt2.x + p2.x, pt2.y + p2.y); _graphics.lineStyle(0, 0x00FF00, 0.6); _graphics.moveTo(pt1.x, pt1.y); _graphics.cubicCurveTo(pt1.x + p1.x, pt1.y + p1.y, pt2.x + p2.x, pt2.y + p2.y, pt2.x, pt2.y); if(isIn){ length = pt0.subtract(pt1).length * _tangentialLineLength; p1.normalize(length); _graphics.lineStyle(0, 0x00FF00, 0.6); _graphics.moveTo(pt0.x, pt0.y); _graphics.cubicCurveTo(pt0.x + (pt1.x - pt0.x) * _tangentialLineLength, pt0.y + (pt1.y - pt0.y) * _tangentialLineLength, pt1.x - p1.x, pt1.y - p1.y, pt1.x, pt1.y); _graphics.lineStyle(0, 0x66FF66, 0.3); _graphics.lineTo(pt1.x - p1.x, pt1.y - p1.y); _graphics.moveTo(pt0.x, pt0.y); _graphics.lineTo(pt0.x + (pt1.x - pt0.x) * _tangentialLineLength, pt0.y + (pt1.y - pt0.y) * _tangentialLineLength); } if (isOut) { length = pt2.subtract(pt3).length * _tangentialLineLength; p2.normalize(length); _graphics.lineStyle(0, 0x00FF00, 0.6); _graphics.moveTo(pt2.x, pt2.y); _graphics.cubicCurveTo(pt2.x - p2.x, pt2.y - p2.y, pt3.x + (pt2.x - pt3.x) * _tangentialLineLength, pt3.y + (pt2.y - pt3.y) * _tangentialLineLength, pt3.x, pt3.y); _graphics.lineStyle(0, 0x66FF66, 0.3); _graphics.lineTo(pt3.x + (pt2.x - pt3.x) * _tangentialLineLength, pt3.y + (pt2.y - pt3.y) * _tangentialLineLength); _graphics.moveTo(pt2.x, pt2.y); _graphics.lineTo(pt2.x - p2.x, pt2.y - p2.y); } } private function drawCurve(pt0:Point, clossPoint:Point, pt1:Point):void { _graphics.moveTo(pt0.x, pt0.y); _graphics.curveTo(clossPoint.x, clossPoint.y, pt1.x, pt1.y); } } // 2次ベジェ曲線(外接) class CircumscribedCurve { private var _graphics:Graphics; public function CircumscribedCurve(graphics:Graphics) { _graphics = graphics; } public function draw(pointList:Array/*Point*/):void { var n:int = pointList.length; if (n > 3) { var m:int = n - 3; for (var i:int = 0; i < m; i++) { drawLine(pointList[i], pointList[i + 1], pointList[i + 2], pointList[i + 3], i == 0, i == (m - 1)); } } } private function drawLine(pt0:Point, pt1:Point, pt2:Point, pt3:Point, isIn:Boolean, isOut:Boolean):void { var p1:Point = Calc.tangentialLine(pt0, pt1, pt2); var p2:Point = Calc.tangentialLine(pt3, pt2, pt1); var clossPoint:Point = Calc.getClossPoint(pt1, pt2, pt1.add(p1), pt2.add(p2)); var cpt1:Point = clossPoint.subtract(pt1); var cpt2:Point = clossPoint.subtract(pt2); if (p1.add(cpt1).length > p1.subtract(cpt1).length && p2.add(cpt2).length > p2.subtract(cpt2).length) { _graphics.lineStyle(0, 0xFF0000, 0.5); drawCurve(pt1, clossPoint, pt2); _graphics.lineStyle(0, 0xFF6666, 0.2); _graphics.moveTo(pt1.x, pt1.y); _graphics.lineTo(clossPoint.x, clossPoint.y); _graphics.lineTo(pt2.x, pt2.y); }else { var p1Radian:Number = Calc.angleBetween(p1, pt2.subtract(pt1)); var p2Radian:Number = Calc.angleBetween(p2, pt1.subtract(pt2)); var ratio:Number = p2Radian / (p1Radian + p2Radian); var radian:Number = (Math.atan2(p1.y, p1.x) + Math.atan2(p2.y, p2.x)) * 0.5; var apt:Point = new Point(pt1.x, pt1.y); var bpt:Point = new Point(pt1.x + p1.x * 30, pt1.y + p1.y * 30); var cpt:Point = new Point((pt1.x * ratio + pt2.x * (1 - ratio)), (pt1.y * ratio + pt2.y * (1 - ratio))); var dpt:Point = new Point((pt1.x * ratio + pt2.x * (1 - ratio)) + Math.cos(radian) * 20, (pt1.y * ratio + pt2.y * (1 - ratio)) + Math.sin(radian) * 20); var ept:Point = new Point(pt2.x, pt2.y); var fpt:Point = new Point(pt2.x + p2.x * 30, pt2.y + p2.y * 30); var c1Point:Point = Calc.getClossPoint(apt, cpt, bpt, dpt); var c2Point:Point = Calc.getClossPoint(ept, cpt, fpt, dpt); _graphics.lineStyle(0, 0xFF0000, 0.5); drawCurve(apt, c1Point, cpt); drawCurve(cpt, c2Point, ept); _graphics.lineStyle(0, 0xFF6666, 0.2); _graphics.moveTo(apt.x, apt.y); _graphics.lineTo(c1Point.x, c1Point.y); _graphics.lineTo(c2Point.x, c2Point.y); _graphics.lineTo(ept.x, ept.y); } var length:Number; var corner:Point; if (isIn) { length = pt0.subtract(pt1).length * 0.25; _graphics.lineStyle(0, 0xFF0000, 0.5); corner = new Point(pt1.x - p1.x * length, pt1.y - p1.y * length); drawCurve(pt0, corner, pt1); _graphics.lineStyle(0, 0xFF6666, 0.2); _graphics.moveTo(pt0.x, pt0.y); _graphics.lineTo(corner.x, corner.y); _graphics.lineTo(pt1.x, pt1.y); } if (isOut) { length = pt2.subtract(pt3).length * 0.25; _graphics.lineStyle(0, 0xFF0000, 0.5); corner = new Point(pt2.x - p2.x * length, pt2.y - p2.y * length) drawCurve(pt2, corner, pt3); _graphics.lineStyle(0, 0xFF6666, 0.2); _graphics.moveTo(pt2.x, pt2.y); _graphics.lineTo(corner.x, corner.y); _graphics.lineTo(pt3.x, pt3.y); } } private function drawCurve(pt0:Point, clossPoint:Point, pt1:Point):void { _graphics.moveTo(pt0.x, pt0.y); _graphics.curveTo(clossPoint.x, clossPoint.y, pt1.x, pt1.y); } } class Calc { public function Calc() { } static public function angleBetween(a:Point,b:Point):Number { return Math.acos((a.x * b.x + a.y * b.y) / (Math.sqrt(a.x * a.x + a.y * a.y) * Math.sqrt(b.x * b.x + b.y * b.y))); } // 折れた直線の接線のベクトルを求める static public function tangentialLine(point0:Point, point1:Point, point2:Point):Point { var pt0:Point = point1.subtract(point0); var pt1:Point = point1.subtract(point2); pt0.normalize(1); pt1.normalize(1); return pt0.subtract(pt1); } // 4点からなる交点 static public function getClossPoint(pa0:Point, pa1:Point, pb0:Point, pb1:Point):Point { var result:Point = new Point(); var s0:Number = ((pb1.x - pa1.x) * (pa0.y - pa1.y) - (pb1.y - pa1.y) * (pa0.x - pa1.x)) * 0.5; var s1:Number = ((pb1.x - pa1.x) * (pa1.y - pb0.y) - (pb1.y - pa1.y) * (pa1.x - pb0.x)) * 0.5; result.x = pa0.x + (pb0.x - pa0.x) * (s0 / (s0 + s1)); result.y = pa0.y + (pb0.y - pa0.y) * (s0 / (s0 + s1)); return result; } } |