Skip to content

Instantly share code, notes, and snippets.

@honzabrecka
Last active August 29, 2015 14:20
Show Gist options
  • Save honzabrecka/b4eb03d7afb0cac5e18a to your computer and use it in GitHub Desktop.
Save honzabrecka/b4eb03d7afb0cac5e18a to your computer and use it in GitHub Desktop.
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Matrix;
var N:uint = 4;
var speed:uint = 1;
var marks:Vector.<Mark> = new Vector.<Mark>(N, true);
var vertices:Vector.<Point> = new Vector.<Point>(N, true);
var center:Point = new Point(rect.x, rect.y);
var matrix:Matrix = new Matrix();
var r:uint = 0;
var i:uint;
vertices[0] = new Point(rect.x, rect.y);
vertices[1] = new Point(rect.x + rect.width, rect.y);
vertices[2] = new Point(rect.x + rect.width, rect.y + rect.height);
vertices[3] = new Point(rect.x, rect.y + rect.height);
matrix.translate(-(rect.width * .5), -(rect.height * .5));
for (i = 0; i < N; i++) {
vertices[i] = matrix.transformPoint(vertices[i]);
marks[i] = new Mark();
marks[i].x = vertices[i].x;
marks[i].y = vertices[i].y;
addChild(marks[i]);
}
function drawBoundingBox():void
{
const quadrant:uint = Math.floor(r / 90);
const X:int = vertices[(3 - quadrant + N) % N].x;
const Y:int = vertices[(0 - quadrant + N) % N].y;
const W:int = vertices[(1 - quadrant + N) % N].x - X;
const H:int = vertices[(2 - quadrant + N) % N].y - Y;
graphics.clear();
graphics.lineStyle(1);
graphics.drawRect(X, Y, W, H);
}
addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
rect.rotation += speed;
matrix = new Matrix();
matrix.translate(-center.x, -center.y);
matrix.rotate(speed * 2 * Math.PI / 360);
matrix.translate(center.x, center.y);
for (i = 0; i < N; i++) {
vertices[i] = matrix.transformPoint(vertices[i]);
marks[i].x = vertices[i].x;
marks[i].y = vertices[i].y;
}
r = (r + speed) % 360;
drawBoundingBox();
});
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Matrix;
var N:uint = 4;
var speed:uint = 1;
var marks:Vector.<Mark> = new Vector.<Mark>(N, true);
var vertices:Vector.<Point> = new Vector.<Point>(N, true);
var center:Point = new Point(rect.x, rect.y);
var matrix:Matrix = new Matrix();
var i:uint;
vertices[0] = new Point(rect.x, rect.y);
vertices[1] = new Point(rect.x + rect.width, rect.y);
vertices[2] = new Point(rect.x + rect.width, rect.y + rect.height);
vertices[3] = new Point(rect.x, rect.y + rect.height);
matrix.translate(-(rect.width * .5), -(rect.height * .5));
for (i = 0; i < N; i++) {
vertices[i] = matrix.transformPoint(vertices[i]);
marks[i] = new Mark();
marks[i].x = vertices[i].x;
marks[i].y = vertices[i].y;
addChild(marks[i]);
}
const LOOKUP:Vector.<Vector.<uint>> = new <Vector.<uint>>[
new <uint>[3, 0, 1, 2],
new <uint>[2, 3, 0, 1],
new <uint>[1, 2, 3, 0],
new <uint>[0, 1, 2, 3],
];
function normalizeRotation(rotation:int):int
{
return (rotation + 360) % 360;
}
function drawBoundingBox():void
{
const quadrant:uint = (normalizeRotation(rect.rotation) / 90) >> 0;
const T:Vector.<uint> = LOOKUP[quadrant];
const X:int = vertices[T[0]].x;
const Y:int = vertices[T[1]].y;
const W:int = vertices[T[2]].x - X;
const H:int = vertices[T[3]].y - Y;
graphics.clear();
graphics.lineStyle(1);
graphics.drawRect(X, Y, W, H);
}
addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
rect.rotation += speed;
matrix = new Matrix();
matrix.translate(-center.x, -center.y);
matrix.rotate(speed * 2 * Math.PI / 360);
matrix.translate(center.x, center.y);
for (i = 0; i < N; i++) {
vertices[i] = matrix.transformPoint(vertices[i]);
marks[i].x = vertices[i].x;
marks[i].y = vertices[i].y;
}
drawBoundingBox();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment