Advanced coordinate rotation - actionscript-3

package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
public class Game2 extends Sprite {
var balls:Array;
var radius:Number = 50;
var centerX:Number = stage.stageWidth / 2;
var centerY:Number = stage.stageHeight / 2;
var i:int = 0;
var angle:Number = 0.1;
var sin:Number = Math.sin(angle);
var cos:Number = Math.cos(angle);
public function Game2() {
init();
}
function init():void
{
balls = new Array();
for(i = 0; i < 8; i++)
{
var ball:Ball = new Ball(10, 0x00FF00);
var xposition = centerX + Math.cos(i / 8 * Math.PI * 2) * radius;
var yposition = centerY + Math.sin(i / 8 * Math.PI * 2) * radius;
ball.x = xposition;
ball.y = yposition;
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(e:Event):void
{
for(i = 0; i < balls.length; i++)
{
var ball:Ball = balls[i];
var x1:Number = ball.x - stage.stageWidth / 2;
var y1:Number = ball.y - stage.stageHeight / 2;
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
ball.x = stage.stageWidth / 2 + x2;
ball.y = stage.stageHeight / 2 + y2;
}
}
}
}
Can someone explain the work of this formula?:
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
i just can't figure it out, if we edit it like this:
var x2:Number = x1 - y1;
var y2:Number = x1 + y1;
all the balls are moving so fast and get out of screen bounds, and also why if we change it like this:
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
or
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
it will work equal, as far as i understanded is that here happens some kind of simetry, if we are on the left or right sides the velocity x == 0 and y is at the maximum velocity of its position the y slows down each time he get closer to the top or bottom, if we are on the top or bottom the velocity y == 0 and x is at maximum speed of his position then also slows down each time he gets closer to the right or left sides, i traced it, but i can't understand why we have to multiply it by cos and sin, i've traced this moments but still can't figure it out, can anyone explain this moment?

The two formulas represent a rotation matrix multiplied with a vector.

Related

Is there a way to rotate the object and then calculate its x and y coordinates as if it was turned around some point without measurement accuracy?

Is there a way to rotate the object with the property rotation, and then calculate its x and y coordinates as if it was turned around some point
without measurement accuracy?
public static function pointRotate (object:DisplayObject, center:Point,angle:Number) : void
{
var r:Number = angle * Math.PI / 180;
var s:Number = Math.sin(r);
var c:Number = Math.cos(r);
var dX:Number = object.x - center.x;
var dY:Number = object.y - center.y;
object.rotation += angle;
object.x = center.x + dX * c - dY * s;
object.y = center.y + dX * s + dY * c;
}
package
{
import flash.geom.Point;
import flash.display.DisplayObject;
public function pointRotate(object:DisplayObject, center:Point, angle:Number) : void
{
// return to zero
angle += object.rotation;
var a0:Number = - object.rotation * Math.PI / 180;
var s0:Number = Math.sin(a0);
var c0:Number = Math.cos(a0);
var dX0:Number = object.x - center.x;
var dY0:Number = object.y - center.y;
object.rotation = 0;
object.x = Math.round(center.x + dX0 * c0 - dY0 * s0);
object.y = Math.round(center.y + dX0 * s0 + dY0 * c0);
// new rotation
var r:Number = angle * Math.PI / 180;
var s:Number = Math.sin(r);
var c:Number = Math.cos(r);
var dX:Number = object.x - center.x;
var dY:Number = object.y - center.y;
object.rotation += angle;
object.x = center.x + dX * c - dY * s;
object.y = center.y + dX * s + dY * c;
}
}

Html5 canvas bouncing balls speed

I have spheres bouncing off each other and the canvas walls, however im struggling on the speed. How would I control the speed? so maybe connected to the beginpath function? so they kind of explode and then bounce everywhere
<script type="text/javascript">
$(function () {
var canvas = $("#target");
var context = canvas.get(0).getContext("2d");
var canvasWidth = canvas.width();
var canvasHeight = canvas.height();
$(window).resize(resizeCanvas);
function resizeCanvas() {
canvas.attr("width", $(window).get(0).innerWidth);
canvas.attr("height", $(window).get(0).innerHeight);
canvasWidth = canvas.width();
canvasHeight = canvas.height();
};
var Sphere = function (x, y, radius, mass, vX, vY) {
this.x = x;
this.y = y;
this.radius = radius;
this.mass = mass;
this.vX = vX;
this.vY = vY;
this.updatePosition = function () {
this.x += this.vX;
this.y += this.vY;
};
this.checkBoundaryCollision = function () {
if (this.x - this.radius < 0) {
this.x = this.radius;
this.vX *= -1;
} else if (this.x + this.radius > canvasWidth) {
this.x = canvasWidth - this.radius;
this.vX *= -1;
}
if (this.y - this.radius < 0) {
this.y = this.radius;
this.vY *= -1;
} else if (this.y + this.radius > canvasHeight) {
this.y = canvasHeight - this.radius;
this.vY *= -1;
}
}
}
resizeCanvas();
var spheresLength = 20;
var spheres = new Array();
function loadContent() {
for (var i = 0; i < spheresLength; i++) {
var x = 20 + (Math.random() * (canvasWidth - 40));
var y = 20 + (Math.random() * (canvasHeight - 40));
var radius = 5 + Math.random() * 10;
var mass = radius / 2;
var vX = Math.random() * 4 - 2;
var vY = Math.random() * 4 - 2;
spheres.push(new Sphere(x, y, radius, mass, vX, vY));
};
animate();
}
loadContent();
function animate() {
update();
draw();
setTimeout(animate, 33);
}
function update() {
for (var i = 0; i < spheresLength; i++) {
var sphere1 = spheres[i];
for (var j = i + 1; j < spheresLength; j++) {
var sphere2 = spheres[j];
var dX = sphere2.x - sphere1.x;
var dY = sphere2.y - sphere1.y;
var distance = Math.sqrt((dX * dX) + (dY * dY));
if (distance < sphere1.radius + sphere2.radius) {
var angle = Math.atan2(dY, dX);
var sine = Math.sin(angle);
var cosine = Math.cos(angle);
var x = 0;
var y = 0;
var xB = dX * cosine + dY * sine;
var yB = dY * cosine - dX * sine;
var vX = sphere1.vX * cosine + sphere1.vY * sine;
var vY = sphere1.vY * cosine - sphere1.vX * sine;
var vXb = sphere2.vX * cosine + sphere2.vY * sine;
var vYb = sphere2.vY * cosine - sphere2.vX * sine;
var vTotal = vX - vXb;
vX = (
(sphere1.mass - sphere2.mass) * vX + 2 * sphere2.mass * vXb
)
/ (sphere1.mass + sphere2.mass);
vXb = vTotal + vX;
xB = x + (sphere1.radius + sphere2.radius);
sphere1.x = sphere1.x + (x * cosine - y * sine);
sphere1.y = sphere1.y + (y * cosine + x * sine);
sphere2.x = sphere1.x + (xB * cosine - yB * sine);
sphere2.y = sphere1.y + (yB * cosine + xB * sine);
sphere1.vX = vX * cosine - vY * sine;
sphere1.vY = vY * cosine + vX * sine;
sphere2.vX = vXb * cosine - vYb * sine;
sphere2.vY = vYb * cosine + vXb * sine;
}
}
sphere1.updatePosition();
sphere1.checkBoundaryCollision();
}
}
function draw() {
context.clearRect(0, 0, canvasWidth, canvasHeight);
context.fillStyle = "rgb(255, 255, 255)";
for (var i = 0; i < spheresLength; i++) {
var sphere = spheres[i];
context.beginPath();
context.arc(sphere.x, sphere.y, sphere.radius, 0, Math.PI * 2);
context.closePath();
context.fill();
}
}
});
</script>
The "speed" of the balls is a function of the distance they travel per frame. This code runs about 3 frames per second, and it looks like the distance is a function of the "mass." You can increase the speed by increasing the factor that determines the distance traveled in each frame (in the update() function.)
Incidentally, I wasn't able to get this code to work. In future, try prototyping on jsFiddle.net or Codepen.io.
UPDATE:
Here's the line that controls the velocity of the balls:
vX = ((sphere1.mass - sphere2.mass) * vX + 2 * sphere2.mass * vXb)
/ (sphere1.mass + sphere2.mass);
That value, 2, is the overall velocity modifier. Small increases to that value correlate to large increases in velocity. Kick it up to 2.25 and watch them fly.

3D Particles Ball

If I'd like to make a circle with particles, I'd define random coordinates for particles like this:
for(var i:int = 0; i != 100; i++)
{
var angle:Number = Math.random() * 360;
var r:Number = 600;
var nX:Number = r * Math.cos(angle / 180 * Math.PI);
var nY:Number = r * Math.sin(angle / 180 * Math.PI);
}
But I want to make a ball with particles on 3d space but I don't know how to define random coordinates for particles. Radius is fixed again but there should be a "nZ" value. Can you help me to define these coordinates?
for(var i:int = 0; i != 100; i++)
{
var angle:Number = Math.random() * 360;
var r:Number = 600;
var nX:Number = ???
var nY:Number = ???
var nZ:Number = ???
}
Thanks in advance...
Spherical coordinates are what you want.
You need two angles:
for(var i:int = 0; i != 100; i++)
{
var azimuthAngle:Number = Math.random() * 360;
var elevationAngle:Number = (Math.random() * 180) - 90;
var r:Number = 600;
var nX:Number =
r * Math.cos(azimuthAngle / 180 * Math.PI) * Math.sin(elevationAngle / 180 * Math.PI);
var nY:Number =
r * Math.sin(azimuthAngle / 180 * Math.PI) * Math.sin(elevationAngle / 180 * Math.PI);
var nZ:Number =
r * Math.cos(elevationAngle / 180 * Math.PI);
}

Type was not found or was not a compile-time constant: Raster

This is the full error message:
C:\Project Files\Good\src\views\RasterView.as(26): col: 39 Error: Type was not found or was not a compile-time constant: Raster.
C:\Project Files\Good\src\views\RasterView.as(42): col: 45 Error: Type was not found or was not a compile-time constant: Raster.
C:\Project Files\Good\src\views\RasterView.as(47): col: 45 Error: Type was not found or was not a compile-time constant: Raster.
C:\Project Files\Good\src\views\RasterView.as(62): col: 32 Error: Type was not found or was not a compile-time constant: Raster.
This is the file(it is huge)
package views
{
import flash.display.Bitmap;
import flash.geom.Point;
import views.Canvas.Raster;
/**
* ...
* #author Arthur Wulf White
*/
public class RasterView extends Bitmap
{
import views.Canvas.Raster
public static const SCHEMATIC : int = 1;
public static const SMOOTH : int = 2;
public static const RENDER_ONCE : int = 4;
public static const BOTH : int = SCHEMATIC + SMOOTH;
// protected var bitmapData : Raster;
protected var viewMode : int = 0;
protected var lineColor : int = 0xff000000;
protected var fillColor : int = 0xff808080;
protected var bgColor : int = 0xffffffff;
public function RasterView(raster : Raster)
{
this.bitmapData = raster;
}
public function getStyle():Vector.<int>
{
return new <int> [lineColor, fillColor, bgColor];
}
public function setStyle(lineColor : int, fillColor : int, bgColor : int):void
{
this.lineColor = lineColor;
this.fillColor = fillColor;
this.bgColor = bgColor;
}
public function backupView(backupRaster : Raster):void
{
backupRaster.copyPixels(bitmapData, bitmapData.rect, new Point());
}
public function loadBackup(backupRaster : Raster):void
{
bitmapData.copyPixels(backupRaster, backupRaster.rect, new Point());
}
public function getViewMode():int
{
return viewMode;
}
public function setViewMode(mode : int):void
{
viewMode = mode;
}
public function get Raster():Raster
{
return Raster(bitmapData);
}
public function refreshView():void
{
bitmapData.lock()
render();
bitmapData.unlock();
if ((getViewMode() & RENDER_ONCE) != 0)
{
setViewMode( getViewMode() ^ RENDER_ONCE);
}
}
public function render():void
{
}
protected function clear():void
{
bitmapData.fillRect(bitmapData.rect, bgColor);
}
}
}
This is the file for the Raster class(I don't think it is important for this issue)
/**
*
* Raster class
*
* #author Didier Brun aka Foxy - www.foxaweb.com
* #version 1.4
* #date 2006-01-06
* #link http://www.foxaweb.com
*
* AUTHORS ******************************************************************************
*
* authorName : Didier Brun - www.foxaweb.com
* contribution : the original class
* date : 2007-01-07
*
* authorName : Drew Cummins - http://blog.generalrelativity.org
* contribution : added bezier curves
* date : 2007-02-13
*
* authorName : Thibault Imbert - http://www.bytearray.org
* contribution : Raster now extends BitmapData, performance optimizations
* date : 2009-10-16
*
* PLEASE CONTRIBUTE ? http://www.bytearray.org/?p=67
*
* DESCRIPTION **************************************************************************
*
* Raster is an AS3 Bitmap drawing library. It provide some functions to draw directly
* into BitmapData instance.
*
* LICENSE ******************************************************************************
*
* This class is under RECIPROCAL PUBLIC LICENSE.
* http://www.opensource.org/licenses/rpl.php
*
* Please, keep this header and the list of all authors
*
*/
package views.Canvas
{
import flash.display.BitmapData;
import flash.display.Shape;
import flash.geom.Point;
import flash.geom.Rectangle;
public class Raster extends BitmapData
{
private var shape : Shape = new Shape();
private var buffer:Array = new Array();
private var r:Rectangle = new Rectangle();
public function Raster ( width:uint, height:uint, transparent:Boolean=false, color:uint=0)
{
super ( width, height, transparent, color);
}
// ------------------------------------------------
//
// ---o public methods
//
// ------------------------------------------------
public function setPoint( p : Point, color : int, size : int = 1):void
{
if(size == 1)
setPixel(p.x, p.y, color);
else if (size > 1)
drawRect(new Rectangle(p.x - size / 2, p.y - size / 2, size, size), color);
}
public function lineAA( x0:int, y0:int, x1:int, y1:int, color:uint ):void
{
var i : int = 0;
var cInt : int;
var cNum : Number;
var div : Number;
var dx : int = Math.abs(x1 - x0);
var dy : int = Math.abs(y1 - y0);
if(dx >= dy)
{
//do x
if (x0 < x1)
{
cInt = x0;
cNum = y0;
}
else
{
cInt = x1;
cNum = y1;
}
div = Number(y1 - y0) / Number(x1 - x0);
setPixel32(cInt, cNum, color);
for (; i <= dx; i++)
{
cInt++;
cNum += div;
setAAPixel(cInt, cNum, color, true, false);
}
}
else
{
//do y
if (y0 < y1)
{
cInt = y0;
cNum = x0;
}
else
{
cInt = y1;
cNum = x1;
}
div = Number(x1 - x0) / Number(y1 - y0);
setPixel32(cNum, cInt, color);
for (; i <= dy; i++)
{
cInt++;
cNum += div;
setAAPixel(cNum, cInt, color, false, true);
}
}
}
public function quadLine(x0 : int, y0 : int, cx : int, cy : int, x1 : int, y1 : int, color : int = 0xff0000ff, detail : int = 20):void
{
var epsilon : Number = 1.0 / Number(detail);
var total : Number = 0;
var x : int = 0;
var y : int = 0;
for (; total <= 1.001; total+=epsilon)
{
x = (1 - total) * ((1 - total) * x0 + total * cx) + total * (total * x1 + (1 - total) * cx);
y = (1 - total) * ((1 - total) * y0 + total * cy) + total * (total * y1 + (1 - total) * cy);
setPixel32(x, y, color);
}
}
/**
* Draws a antialias Quadratic Bezier Curve (equivalent to a DisplayObject's graphics#curveTo)
*
* #param x0 x position of first anchor
* #param y0 y position of first anchor
* #param x1 x position of control point
* #param y1 y position of control point
* #param x2 x position of second anchor
* #param y2 y position of second anchor
* #param c color
* #param resolution [optional] determines the accuracy of the curve's length (higher number = greater accuracy = longer process)
* */
public function aaQuadBezier ( anchorX0:int, anchorY0:int, controlX:int, controlY:int, anchorX1:int, anchorY1:int, c:Number, resolution:int = 3):void
{
shape.graphics.clear();
shape.graphics.lineStyle(1, c, 1);
shape.graphics.moveTo(anchorX0, anchorY0);
shape.graphics.curveTo(controlX, controlY, anchorX1, anchorY1);
this.draw(shape);
}
/**
* Draw an anti-aliased line
*
* #param x0 first point x coord
* #param y0 first point y coord
* #param x1 second point x coord
* #param y1 second point y coord
* #param c color (0xaarrvvbb)
*/
public function aaLine2( x1:int, y1:int, x2:int, y2:int, color:uint ):void
{
shape.graphics.clear();
shape.graphics.lineStyle(1, color, 1);
shape.graphics.moveTo(x1, y1);
shape.graphics.lineTo(x2, y2);
this.draw(shape);
}
/**
* Draw a line
*
* #param x0 first point x coord
* #param y0 first point y coord
* #param x1 second point x coord
* #param y1 second point y coord
* #param c color (0xaarrvvbb)
*/
public function line ( x0:int, y0:int, x1:int, y1:int, color:uint ):void
{
var dx:int;
var dy:int;
var i:int;
var xinc:int;
var yinc:int;
var cumul:int;
var x:int;
var y:int;
x = x0;
y = y0;
dx = x1 - x0;
dy = y1 - y0;
xinc = ( dx > 0 ) ? 1 : -1;
yinc = ( dy > 0 ) ? 1 : -1;
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
setPixel32(x,y,color);
if ( dx > dy )
{
cumul = dx >> 1;
for ( i = 1 ; i <= dx ; ++i )
{
x += xinc;
cumul += dy;
if (cumul >= dx)
{
cumul -= dx;
y += yinc;
}
setPixel32(x,y,color);
}
}else
{
cumul = dy >> 1;
for ( i = 1 ; i <= dy ; ++i )
{
y += yinc;
cumul += dx;
if ( cumul >= dy )
{
cumul -= dy;
x += xinc ;
}
setPixel32(x,y,color);
}
}
}
/**
* Draw a triangle
*
* #param x0 first point x coord
* #param y0 first point y coord
* #param x1 second point x coord
* #param y1 second point y coord
* #param x2 third point x coord
* #param y2 third point y coord
* #param c color (0xaarrvvbb)
*/
public function triangle ( x0:int, y0:int, x1:int, y1:int, x2:int, y2:int, color:uint ):void
{
line (x0,y0,x1,y1,color);
line (x1,y1,x2,y2,color);
line (x2,y2,x0,y0,color);
}
/**
* Draw a filled triangle
*
* #param x0 first point x coord
* #param y0 first point y coord
* #param x1 second point x coord
* #param y1 second point y coord
* #param x2 third point x coord
* #param y2 third point y coord
* #param c color (0xaarrvvbb)
*/
public function filledTri ( x0:int, y0:int, x1:int, y1:int, x2:int, y2:int, color:uint ):void
{
buffer.length = 0;
lineTri (buffer,x0,y0,x1,y1,color);
lineTri (buffer,x1,y1,x2,y2,color);
lineTri (buffer,x2,y2,x0,y0,color);
}
/**
* Draw a circle
*
* #param px first point x coord
* #param py first point y coord
* #param r radius
* #param c color (0xaarrvvbb)
*/
public function circle ( px:int, py:int, r:int, color:uint ):void
{
var x:int;
var y:int;
var d:int;
x = 0;
y = r;
d = 1-r;
setPixel32(px+x,py+y,color);
setPixel32(px+x,py-y,color);
setPixel32(px-y,py+x,color);
setPixel32(px+y,py+x,color);
while ( y > x )
{
if ( d < 0 )
{
d += (x+3) << 1;
}else
{
d += ((x - y) << 1) + 5;
y--;
}
x++;
setPixel32(px+x,py+y,color);
setPixel32(px-x,py+y,color);
setPixel32(px+x,py-y,color);
setPixel32(px-x,py-y,color);
setPixel32(px-y,py+x,color);
setPixel32(px-y,py-x,color);
setPixel32(px+y,py-x,color);
setPixel32(px+y,py+x,color);
}
}
/**
* Draw an anti-aliased circle
*
* #param px first point x coord
* #param py first point y coord
* #param r radius
* #param c color (0xaarrvvbb)
*/
public function aaCircle ( px:int, py:int, r:int, color:uint ):void
{
var vx:int;
var vy:int;
var d:int;
vx = r;
vy = 0;
var t:Number=0;
var dry:Number;
var buff:int;
setPixel(px+vx,py+vy,color);
setPixel(px-vx,py+vy,color);
setPixel(px+vy,py+vx,color);
setPixel(px+vy,py-vx,color);
while ( vx > vy+1 )
{
vy++;
buff = Math.sqrt(r*r-vy*vy)+1;
dry = buff - Math.sqrt(r*r-vy*vy);
if (dry<t) vx--;
drawAlphaPixel(px+vx,py+vy,1-dry,color)
drawAlphaPixel(px+vx-1,py+vy,dry,color)
drawAlphaPixel(px-vx,py+vy,1-dry,color)
drawAlphaPixel(px-vx+1,py+vy,dry,color)
drawAlphaPixel(px+vx,py-vy,1-dry,color)
drawAlphaPixel(px+vx-1,py-vy,dry,color)
drawAlphaPixel(px-vx,py-vy,1-dry,color)
drawAlphaPixel(px-vx+1,py-vy,dry,color)
drawAlphaPixel(px+vy,py+vx,1-dry,color)
drawAlphaPixel(px+vy,py+vx-1,dry,color)
drawAlphaPixel(px-vy,py+vx,1-dry,color)
drawAlphaPixel(px-vy,py+vx-1,dry,color)
drawAlphaPixel(px+vy,py-vx,1-dry,color)
drawAlphaPixel(px+vy,py-vx+1,dry,color)
drawAlphaPixel(px-vy,py-vx,1-dry,color)
drawAlphaPixel(px-vy,py-vx+1,dry,color)
t=dry;
}
}
/**
* Draw an anti-aliased line
*
* #param x0 first point x coord
* #param y0 first point y coord
* #param x1 second point x coord
* #param y1 second point y coord
* #param c color (0xaarrvvbb)
*/
public function aaLine ( x1:int, y1:int, x2:int, y2:int, color:uint ):void
{
var steep:Boolean = Math.abs(y2 - y1) > Math.abs(x2 - x1);
var swap:int;
if (steep)
{
swap=x1; x1=y1; y1=swap;
swap=x2; x2=y2; y2=swap;
}
if (x1 > x2)
{
swap=x1; x1=x2; x2=swap;
swap=y1; y1=y2; y2=swap;
}
var dx:int = x2 - x1;
var dy:int = y2 - y1
var gradient:Number = dy / dx;
var xend:int = x1;
var yend:Number = y1 + gradient * (xend - x1);
var xgap:Number = 1-((x1 + 0.5)%1);
var xpx1:int = xend;
var ypx1:int = yend;
var alpha:Number;
alpha = ((yend)%1) * xgap;
var intery:Number = yend + gradient;
xend = x2;
yend = y2 + gradient * (xend - x2)
xgap = (x2 + 0.5)%1;
var xpx2:int = xend;
var ypx2:int = yend;
alpha = (1-((yend)%1)) * xgap;
if (steep)
drawAlphaPixel(ypx2,xpx2,alpha,color);
else drawAlphaPixel(xpx2, ypx2,alpha,color);
alpha = ((yend)%1) * xgap;
if (steep)
drawAlphaPixel(ypx2 + 1,xpx2,alpha,color);
else drawAlphaPixel(xpx2, ypx2 + 1,alpha,color);
var x:int=xpx1;
while (x++<xpx2)
{
alpha = 1-((intery)%1);
if (steep)
drawAlphaPixel(intery,x,alpha,color);
else drawAlphaPixel(x,intery,alpha,color);
alpha=intery%1;
if (steep)
drawAlphaPixel(intery+1,x,alpha,color);
else drawAlphaPixel(x,intery+1,alpha,color);
intery = intery + gradient
}
}
/**
* Draws a Rectangle
*
* #param rect Rectangle dimensions
* #param color color
* */
public function drawRect ( rect:Rectangle, color:uint ):void
{
line ( rect.x, rect.y, rect.x+rect.width, rect.y, color );
line ( rect.x+rect.width, rect.y, rect.x+rect.width, rect.y+rect.height, color );
line ( rect.x+rect.width, rect.y+rect.height, rect.x, rect.y+rect.height, color );
line ( rect.x, rect.y+rect.height, rect.x, rect.y, color );
}
/**
* Draws a rounded Rectangle
*
* #param rect Rectangle dimensions
* #param ellipseWidth Rectangle corners width
* #param color color
* */
public function drawRoundRect ( rect:Rectangle, ellipseWidth:int, color:uint ):void
{
var arc:Number = 4/3 * (Math.sqrt(2) - 1);
var xc:Number = rect.x+rect.width-ellipseWidth;
var yc:Number = rect.y+ellipseWidth;
line( rect.x+ellipseWidth, rect.y, xc, rect.y, color );
cubicBezier(xc, rect.y, xc + ellipseWidth*arc, yc - ellipseWidth, xc + ellipseWidth, yc - ellipseWidth*arc, xc + ellipseWidth, yc, color);
xc = rect.x+rect.width-ellipseWidth;
yc = rect.y+rect.height-ellipseWidth;
line( xc + ellipseWidth, rect.y+ellipseWidth, rect.x+rect.width, yc, color );
cubicBezier(rect.x+rect.width, yc, xc + ellipseWidth, yc + ellipseWidth*arc, xc + ellipseWidth*arc, yc + ellipseWidth, xc, yc + ellipseWidth, color);
xc = rect.x+ellipseWidth;
yc = rect.y+rect.height-ellipseWidth;
line( rect.x+rect.width-ellipseWidth, rect.y+rect.height, xc, yc + ellipseWidth, color );
cubicBezier( xc, yc + ellipseWidth, xc - ellipseWidth*arc, yc + ellipseWidth, xc - ellipseWidth, yc + ellipseWidth*arc, xc - ellipseWidth, yc, color );
xc = rect.x+ellipseWidth;
yc = rect.y+ellipseWidth;
line( xc - ellipseWidth, rect.y+rect.height-ellipseWidth, rect.x, yc, color );
cubicBezier(rect.x, yc, xc - ellipseWidth, yc - ellipseWidth*arc, xc - ellipseWidth*arc, yc - ellipseWidth, xc, yc - ellipseWidth, color);
}
/**
* Draws a Quadratic Bezier Curve (equivalent to a DisplayObject's graphics#curveTo)
*
* #param x0 x position of first anchor
* #param y0 y position of first anchor
* #param x1 x position of control point
* #param y1 y position of control point
* #param x2 x position of second anchor
* #param y2 y position of second anchor
* #param c color
* #param resolution [optional] determines the accuracy of the curve's length (higher number = greater accuracy = longer process)
* */
public function quadBezier ( anchorX0:int, anchorY0:int, controlX:int, controlY:int, anchorX1:int, anchorY1:int, c:Number, resolution:int = 3):void
{
var ox:Number = anchorX0;
var oy:Number = anchorY0;
var px:int;
var py:int;
var dist:Number = 0;
var inverse:Number = 1 / resolution;
var interval:Number;
var intervalSq:Number;
var diff:Number;
var diffSq:Number;
var i:int = 0;
while( ++i <= resolution )
{
interval = inverse * i;
intervalSq = interval * interval;
diff = 1 - interval;
diffSq = diff * diff;
px = diffSq * anchorX0 + 2 * interval * diff * controlX + intervalSq * anchorX1;
py = diffSq * anchorY0 + 2 * interval * diff * controlY + intervalSq * anchorY1;
dist += Math.sqrt( ( px - ox ) * ( px - ox ) + ( py - oy ) * ( py - oy ) );
ox = px;
oy = py;
}
//approximates the length of the curve
var curveLength:int = dist;
inverse = 1 / curveLength;
var lastx:int=anchorX0;
var lasty:int=anchorY0;
i = -1;
while( ++i <= curveLength )
{
interval = inverse * i;
intervalSq = interval * interval;
diff = 1 - interval;
diffSq = diff * diff;
px = diffSq * anchorX0 + 2 * interval * diff * controlX + intervalSq * anchorX1;
py = diffSq * anchorY0 + 2 * interval * diff * controlY + intervalSq * anchorY1;
line(lastx,lasty,px,py,c);
//aaLine(lastx, lasty, px, py, c);
lastx = px;
lasty = py;
}
}
/**
* Draws a Cubic Bezier Curve
*
* TODO: Determine whether x/y params would be better named as anchor/control
*
* #param x0 x position of first anchor
* #param y0 y position of first anchor
* #param x1 x position of control point
* #param y1 y position of control point
* #param x2 x position of second control point
* #param y2 y position of second control point
* #param x3 x position of second anchor
* #param y3 y position of second anchor
* #param c color
* #param resolution [optional] determines the accuracy of the curve's length (higher number = greater accuracy = longer process)
* */
public function cubicBezier ( x0:int, y0:int, x1:int, y1:int, x2:int, y2:int, x3:int, y3:int, c:Number, resolution:int = 5 ):void
{
var ox:Number = x0;
var oy:Number = y0;
var px:int;
var py:int;
var dist:Number = 0;
var inverse:Number = 1 / resolution;
var interval:Number;
var intervalSq:Number;
var intervalCu:Number;
var diff:Number;
var diffSq:Number;
var diffCu:Number;
var i:int = 0;
while( ++i <= resolution )
{
interval = inverse * i;
intervalSq = interval * interval;
intervalCu = intervalSq * interval;
diff = 1 - interval;
diffSq = diff * diff;
diffCu = diffSq * diff;
px = diffCu * x0 + 3 * interval * diffSq * x1 + 3 * x2 * intervalSq * diff + x3 * intervalCu;
py = diffCu * y0 + 3 * interval * diffSq * y1 + 3 * y2 * intervalSq * diff + y3 * intervalCu;
dist += Math.sqrt( ( px - ox ) * ( px - ox ) + ( py - oy ) * ( py - oy ) );
ox = px;
oy = py;
}
//approximates the length of the curve
var curveLength:int = dist;
inverse = 1 / curveLength;
var lastx:int=x0;
var lasty:int=y0;
i = -1;
while( ++i <= curveLength )
{
interval = inverse * i;
intervalSq = interval * interval;
intervalCu = intervalSq * interval;
diff = 1 - interval;
diffSq = diff * diff;
diffCu = diffSq * diff;
px = diffCu * x0 + 3 * interval * diffSq * x1 + 3 * x2 * intervalSq * diff + x3 * intervalCu;
py = diffCu * y0 + 3 * interval * diffSq * y1 + 3 * y2 * intervalSq * diff + y3 * intervalCu;
line(lastx,lasty,px,py,c);
lastx = px;
lasty = py;
}
}
// ------------------------------------------------
//
// ---o private static methods
//
// ------------------------------------------------
/**
* Draw an AA pixel
*/
private function setAAPixel (x:Number, y:Number, c:Number, roundX : Boolean = false, roundY : Boolean = false):void
{
var xpos : Number = Math.floor(x);
var ypos : Number = Math.floor(y);
var xA : Number = x - xpos;
var yA : Number = y - ypos;
if (!roundX && !roundY)
{
drawAlphaPixel(xpos , ypos , (1 - xA) * (1 - yA) , c);
drawAlphaPixel(xpos + 1 , ypos , xA * (1 - yA) , c);
drawAlphaPixel(xpos , ypos + 1 , (1 - xA) * yA , c);
drawAlphaPixel(xpos + 1 , ypos + 1 , xA * yA , c);
}
else if (roundX && !roundY)
{
drawAlphaPixel(xpos , ypos , (1 - yA) , c);
drawAlphaPixel(xpos , ypos + 1 , yA , c);
}
else if (roundY && !roundX)
{
drawAlphaPixel(xpos , ypos , (1 - xA) , c);
drawAlphaPixel(xpos + 1 , ypos , xA , c);
}
else if (roundX && roundY)
{
setPixel32(xpos , ypos , c);
}
}
/**
* Draw an alpha32 pixel
*/
private function drawAlphaPixel ( x:int, y:int, a:Number, c:Number, bg : int = -1 ):void
{
//var g:uint = getPixel32(x,y);
var g : int = 0xff000000;
var r0:uint = ((g & 0x00FF0000) >> 16);
var g0:uint = ((g & 0x0000FF00) >> 8);
var b0:uint = ((g & 0x000000FF));
var r1:uint = ((c & 0x00FF0000) >> 16);
var g1:uint = ((c & 0x0000FF00) >> 8);
var b1:uint = ((c & 0x000000FF));
var ac:Number = 0xFF;
var rc:Number = r1*a+r0*(1-a);
var gc:Number = g1*a+g0*(1-a);
var bc:Number = b1*a+b0*(1-a);
var n:uint = (ac<<24)+(rc<<16)+(gc<<8)+bc;
setPixel32(x,y,n);
}
/**
* Check a triangle line
*/
private function checkLine ( o:Array, x:int, y:int, c:int, r:Rectangle ):void
{
if (o[y])
{
if (o[y]>x)
{
r.width=o[y]-x;
r.x=x;
r.y=y;
fillRect(r,c);
}else
{
r.width=x-o[y];
r.x=o[y];
r.y=y;
fillRect(r,c);
}
}else
{
o[y]=x;
}
}
/**
* Special line for filled triangle
*/
private function lineTri ( o:Array, x0:int, y0:int, x1:int, y1:int, c:Number ):void
{
var steep:Boolean= (y1-y0)*(y1-y0) > (x1-x0)*(x1-x0);
var swap:int;
if (steep)
{
swap=x0; x0=y0; y0=swap;
swap=x1; x1=y1; y1=swap;
}
if (x0>x1)
{
x0^=x1; x1^=x0; x0^=x1;
y0^=y1; y1^=y0; y0^=y1;
}
var deltax:int = x1 - x0
var deltay:int = (y1 - y0) < 0 ? -(y1 - y0) : (y1 - y0);
var error:int = 0;
var y:int = y0;
var ystep:int = y0<y1 ? 1 : -1;
var x:int = x0;
var xend:int = x1-(deltax>>1);
var fx:int = x1;
var fy:int = y1;
var px:int = 0;
r.x = 0;
r.y = 0;
r.width = 0;
r.height = 1;
while (x++<=xend)
{
if (steep)
{
checkLine(o,y,x,c,r);
if (fx != x1 && fx != xend)
checkLine(o,fy,fx+1,c,r);
}
error += deltay;
if ((error<<1) >= deltax)
{
if (!steep)
{
checkLine(o,x-px+1,y,c,r);
if (fx!=xend)
checkLine(o,fx+1,fy,c,r);
}
px = 0;
y += ystep;
fy -= ystep;
error -= deltax;
}
px++;
fx--;
}
if (!steep)
checkLine(o,x-px+1,y,c,r);
}
}
}
Looks like just a typo or copy/paste error. The import statement should not be repeated inside your class definition:
package views
{
import flash.display.Bitmap;
import flash.geom.Point;
import views.Canvas.Raster; // Yes
/**
* ...
* #author Arthur Wulf White
*/
public class RasterView extends Bitmap
{
import views.Canvas.Raster // No
Also, avoid having an accessor with the same name as a class:
public function get Raster():Raster // Call this function get MyRaster or similar
{
return Raster(bitmapData);
}
Code highlighting is your friend. Any time a property or function name other than your constructor lights up in Cyan, it means you're using an existing class name, which may cause problems.

using action script how to draw a semi circle

using action script how to draw a semi circle...i need to add tha semicircle in another circle the circle looks like this
![alt text][1]
how to draw a semi circle inside that circle
Use the following function to draw the required arc.
function drawArc(centerX, centerY, radius, startAngle, arcAngle, steps){
startAngle -= .25;
var twoPI = 2 * Math.PI;
var angleStep = arcAngle/steps;
var xx = centerX + Math.cos(startAngle * twoPI) * radius;
var yy = centerY + Math.sin(startAngle * twoPI) * radius;
moveTo(xx, yy);
for(var i=1; i<=steps; i++){
var angle = startAngle + i * angleStep;
xx = centerX + Math.cos(angle * twoPI) * radius;
yy = centerY + Math.sin(angle * twoPI) * radius;
lineTo(xx, yy);
}
}
lineStyle(0, 0xFF0000);
drawArc(250, 250, 200, 45/360, -90/360, 20);
Semicircle? Well its joining the end points isn't it. Use lineto.
Thank you so much loxxy.. I have made a few alteration and got dashed line too.
function drawArc(centerX, centerY, radius, startAngle, arcAngle, steps){
centerY=centerY+radius
startAngle -= .25;
var twoPI = 2 * Math.PI;
var angleStep = arcAngle/steps;
trace(angleStep)
var xx = centerX + Math.cos(startAngle * twoPI) * radius;
var yy = centerY + Math.sin(startAngle * twoPI) * radius;
mc.graphics.moveTo(xx, yy);
for(var i=1; i<=steps; i++){
var angle = startAngle + i * angleStep;
xx = centerX + Math.cos(angle * twoPI) * radius;
yy = centerY + Math.sin(angle * twoPI) * radius;
if(i%2==0){
mc.graphics.moveTo(xx, yy);
}else{
mc.graphics.lineTo(xx, yy);
}
}
}