FORUMS: list search recent posts

Math: Add beveled corners to polygon script

COW Forums : Adobe Flash

FAQ   •   VIEW ALL   •   ADD A NEW POST   •   PRINT
Respond to this post   •   Return to posts index


Jakob Wagner
Math: Add beveled corners to polygon script
on Mar 26, 2017 at 11:57:46 am

Hi

I have this script which can position a number of objects in a polygon or star shape. Great!

The only problem is I would like to be able to bevel the corners, or to say it in another way, create some roundness on them.

Can anybody here figure out the math in that?
Let me know if I need to explain more.

Thanks,
Jakob

The script:

package
{
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;

public class Polygon extends MovieClip
{
private var numberOfMarkers;
private var markers:Sprite;
private var offset:Number;
private var numberOfSides:int;
private var polygonAngle:Number;

public function Polygon()
{
numberOfMarkers = 50;
offset = 0;
numberOfSides = 5;
polygonAngle = 90;

markers = new Sprite();
addChild(markers);

for(var i:int=0;i 2)
{
var points:Array = [];

var step:Number, halfStep:Number, start:Number, n:Number, dx:Number, dy:Number;
step = (Math.PI * 2) / spikes;
halfStep = step / 2;
start = (angle / 180) * Math.PI;
points[0] = new Point(x + (Math.cos(start) * outerRadius), y - (Math.sin(start) * outerRadius));

for (var i:int=1;i<=spikes;++i)
{
dx = x + Math.cos(start + (step * i) - halfStep) * innerRadius;
dy = y - Math.sin(start + (step * i) - halfStep) * innerRadius;
points[(i-1)*2+1] = new Point(dx, dy);
dx = x + Math.cos(start + (step * i)) * outerRadius;
dy = y - Math.sin(start + (step * i)) * outerRadius;
points[(i-1)*2+2] = new Point(dx, dy);
}

atAngle = atAngle%360;

var sideLength:Number = Point.distance(points[0], points[1]);
var totalLength:Number = sideLength*spikes*2;
var distanceFromStart:Number = totalLength*atAngle/360;
var startPoint:int = Math.floor(distanceFromStart/sideLength);
var endPoint:int = startPoint==spikes*2-1?0:startPoint+1;
var distanceFromStartPoint:Number = distanceFromStart-(sideLength*startPoint);

var a:Number = points[startPoint].x-points[endPoint].x;
var b:Number = points[startPoint].y-points[endPoint].y;
var A:Number = Math.atan(a/b);
var na:Number = Math.sin(A)*distanceFromStartPoint*(b<0?1:-1);
var nb:Number = Math.cos(A)*distanceFromStartPoint*(b<0?1:-1);

target.x = points[startPoint].x+na;
target.y = points[startPoint].y+nb;
target.rotation = 90-A/(Math.PI/180);
}
}

public function positionInPolygon(target:Sprite, x:Number, y:Number, sides:uint, radius:Number, angle:Number, atAngle:Number):void
{
if (sides <= 2)
{
throw ArgumentError("DrawingShapes.drawPolygon() - parameter 'sides' needs to be atleast 3");
return;
}
if (sides > 2)
{
var points:Array = [];
var step:Number, start:Number, dx:Number, dy:Number;
step = (Math.PI * 2) / sides;
start = (angle / 180) * Math.PI;
points[0] = new Point(x + (Math.cos(start) * radius), y - (Math.sin(start) * radius));

for (var i:int=1;i<=sides;++i)
{
dx = x + Math.cos(start + (step * i)) * radius;
dy = y - Math.sin(start + (step * i)) * radius;
points[i] = new Point(dx, dy);
}

atAngle = atAngle%360;

var sideLength:Number = 2*radius*Math.sin(Math.PI/sides);
var totalLength:Number = sideLength*sides;
var distanceFromStart:Number = totalLength*atAngle/360;
var startPoint:int = Math.floor(distanceFromStart/sideLength);
var endPoint:int = startPoint==sides-1?0:startPoint+1;
var distanceFromStartPoint:Number = distanceFromStart-(sideLength*startPoint);

var a:Number = points[startPoint].x-points[endPoint].x;
var b:Number = points[startPoint].y-points[endPoint].y;
var A:Number = Math.atan(a/b);
var na:Number = Math.sin(A)*distanceFromStartPoint*(b<0?1:-1);
var nb:Number = Math.cos(A)*distanceFromStartPoint*(b<0?1:-1);

target.x = points[startPoint].x+na;
target.y = points[startPoint].y+nb;
target.rotation = 90-A/(Math.PI/180);
}
}
}
}


Posts Index 


Current Message Thread:
  • Math: Add beveled corners to polygon script by Jakob Wagner on Mar 26, 2017 at 11:57:46 am





© 2018 CreativeCOW.net All Rights Reserved
[TOP]