Skip to content

Instantly share code, notes, and snippets.

@VCone
Created April 22, 2016 11:27
Show Gist options
  • Save VCone/fb7d8df5337c293a3fd3476b6fedf76d to your computer and use it in GitHub Desktop.
Save VCone/fb7d8df5337c293a3fd3476b6fedf76d to your computer and use it in GitHub Desktop.
Rectangular to Polar conversion (AS3 code) by Valerio Charles (https://vcone.co.uk). Inspired by Adobe Photoshop
private function convert_RectToPolar ( input_BMP : Bitmap ) : void
{
//# *** NOTES ***
//# "output_canvas" (used later) is the Sprite container for the "input_BMP" (source image)
//# ****************************************************************************************
trace("Doing Function : Rectangular To Polar convert");
var x : int = 0; var y : int = 0;
var Angle : Number = 0; var Theta : Number = 0; var Dist : Number = 0;
var px : Number = 0; var py : Number = 0;
var newX : int = 0; var newY : int = 0;
var xx : int = 0; var yy : int = 0;
var Q : Number = 0; var R : Number = 0;
// get size information
var w:Number = input_BMP.width;
var h:Number = input_BMP.height;
var centerX : Number = 0; var centerY : Number = 0;
var pW:Number = input_BMP.width;
var pH:Number = input_BMP.height;
var lowest_SIZE : int = 0;
//# Temp BitmapData object
var bmd_Temp : BitmapData;
bmd_Temp = new BitmapData( pW, pH, false, 0xFFAA00CC );
centerX = pW / 2;
centerY = pH / 2;
//# Begin convert to Polar Coordinates
for ( y= 0; y <= pH ; y++)
{
for ( x = 0; x <= pW ; x++)
{
centerX = (pW / 2); centerY = (pH / 2);
A = (x - pW / 2); B = (y - pH / 2);
Dist = (Math.sqrt(A * A + B * B) * 2.0);
//# ANGLE is 360 + N where N is origin.. try +0 -VS- +45 -VS- +90
//# Use +90 to make rotation of circle begin in top..
Angle = Math.atan2( B, -A ) * ( 180 / Math.PI);
Angle = ( ( 360 + 90) + Angle ) % 360;
//# Set Q and R
Q = px = Angle * pW / 360;
if ( pH >= pW ) { R = py = Dist / (pW / pH) ; } //#for large HEIGHT
else { R = py = (Dist ); } //#for large WIDTH
//# Stretch to image corners
if ( pW >= pH ) { lowest_SIZE = pH; }
else { lowest_SIZE = pW; }
if ( Dist >= lowest_SIZE )
{
xB = Q;
yB = pH-Dist / pH / Math.PI;
bmd_Temp.setPixel( x , y , input_BMP.bitmapData.getPixel( xB, yB ) );
}
else
{ bmd_Temp.setPixel( x , y , input_BMP.bitmapData.getPixel( Q, R ) ); }
}//# end for loop pW
}//# end for loop pH
//# VISIBILITY
input_BMP.bitmapData = bmd_Temp; //update "Input" BMP data as effected "temp" BMP data
input_BMP.visible = true;
input_BMP.height = pH;
//# Rectangle CROP marker ( a semi-transparent rectangle marking the cropping window/area)
var rectMarker:Shape = new Shape; // initializing the variable named rectangle
rectMarker.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectMarker.graphics.drawRect( 0, 0, 10, 10 ); // (x spacing, y spacing, width, height)
rectMarker.graphics.endFill(); // not always needed but I like to put it in to end the fill
rectMarker.alpha = 0;
if ( pW >= pH ) { rectMarker.width = rectMarker.height = pH; }
else { rectMarker.width = rectMarker.height = pW; }
//CENTRALIZE
temp_BMD = new BitmapData( rectMarker.width , rectMarker.height, false, 0x000000 ); //non-transparent + black bg
temp_BMP = new Bitmap(temp_BMD);
rectMarker.x = (temp_BMP.width - rectMarker.width ) / 2;
rectMarker.y = temp_BMP.y + (temp_BMP.height - rectMarker.height ) / 2;
//# Create a temp Integer to control positioning of scaled copy
var temp_INT : int = pW; //reset to width (also matches height if square)
if (pW >= pH) { temp_INT = (pW - pH) / 2 }
else { temp_INT = (pH - pW) / 2 }
//# Create scaling Matrix (if Width is larger adjust width pos or vice versa)
var matrix:Matrix;
if (pW >= pH) { matrix = new Matrix( 1, 0, 0, 1, -temp_INT , 0 ); }
else { matrix = new Matrix( 1, 0, 0, 1, 0 , -temp_INT ); }
//# Create cropping rectangle
var rectCrop : Rectangle = new Rectangle(0, 0, rectMarker.width, rectMarker.height );
//# Draw a scaled version fit to within edges of display canvas
temp_BMD.draw( input_BMP, matrix, null, null, rectCrop, true);
input_BMP.bitmapData = temp_BMD;
input_BMP.width = pW; input_BMP.height = pH;
matrix = new Matrix( 1, 0, 0, 1, 0, 0 );
temp_BMD = new BitmapData( pW , pH, false, 0x000000 );
temp_BMD.draw( input_BMP, matrix, null, null, null, true);
temp_BMP.smoothing = true;
input_BMP.bitmapData = temp_BMD;
//# NOTE: output_canvas is a Sprite that contained the input BMP
output_canvas.width = pW; output_canvas.height = pH;
temp_BMP = BMP_copy (output_canvas);
temp_BMP.smoothing = true;
} //# END FUNCTION : RECTANGULAR TO POLAR CONVERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment