Polygon Bitmap

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Polygon Bitmap

Post by diki »

A BitmapExpression for simple n-sided filled Polygons, with anti-aliasing, for square bitmaps:

Code: Select all

// bitmap variables

int width = 128;
float aawidth = 2;

// algo variables

int sprockets = 7;
int centeroffset = 1;

// default color

this.pixel.r = 1;
this.Pixel.g = 1;
this.pixel.b = 1;
this.Pixel.A = 0;

//

// other variables

float this_angle;
int this_sprocket;
float normalx, normaly;
float distance;

float aadist = aawidth / width;
float angle =( 2 * PI )/ sprockets;
float maxdist = cos( angle / 2.0 )* 0.5;
float center = 0.5 -( 1.0 * centeroffset / width );

//

// determine angle of current pixel to center
if( this.x - 0.5 >= 0 )
  this_angle = atan2(( this.x - 0.5 ),( this.y - center ));
else
  this_angle = PI + atan2(( 0.5 - this.x ),( center - this.y  ));

// determine sprocket of current pixel
this_sprocket = (( this_angle )/ angle );

// calculate normal of current sprocket
normalx = sin(( angle * this_sprocket )+( angle / 2.0 ));
normaly = cos(( angle * this_sprocket )+( angle / 2.0 ));

// calculate distance of current pixel by dot product with normal
distance =(( this.x - 0.5 )* normalx )+(( this.Y - center )* normaly );

// set alpha accordingly
if( distance < maxdist ) this.pixel.a = ( maxdist - distance )/ aadist;

this.Pixel.R *= this.Pixel.A;
this.Pixel.G *= this.Pixel.A;
this.Pixel.B *= this.Pixel.A;
Attachments
three sample outputs
three sample outputs
output.gif (4.24 KiB) Viewed 6159 times
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post by jph_wacheski »

Thanks for this diki, I have wundered about how to draw a triangle (n-gon) using the bitmap pixel space,. however trig is not my best understood subject,. although with cool examples like this, for me to hack around with, I do keep learning interesting things,. I for one will put this to good use.

I have found that adding size here;

Code: Select all

distance =(( this.x - 0.5 )* normalx )+(( this.Y - center )* normaly );

Code: Select all

distance =((( this.x - 0.5 )* normalx )+(( this.Y - center )* normaly ))*size;
I can scale the figure,. . cheers.
iterationGAMES.com
Post Reply