Snippet
/// <summary>
/// Specifies the direction the 3D object will be drawn.
/// </summary>
public enum Direction3D
{
/// <summary>
/// The cube is angled to the lower left.
/// </summary>
DiagonalDownLeft,
/// <summary>
/// The cube is angled to the lower right.
/// </summary>
DiagonalDownRight,
/// <summary>
/// The cube is angled to the upper left.
/// </summary>
DiagonalUpLeft,
/// <summary>
/// The cube is angled to the upper right.
/// </summary>
DiagonalUpRight
}
/// <summary>
/// Draws a Rectangluar Prism, then returns it as a Bitmap.
/// </summary>
/// <param name="g">The graphics to use.</param>
/// <param name="frontColor">The color of the front of the rectangular prism.</param>
/// <param name="leftRightColor">The color on the left / right (depending on Direction3D) of the rectangular prism.</param>
/// <param name="topBottomColor">The color on the top / bottom (depending on Direction3D) of the rectangular prism.</param>
/// <param name="width">The width of the rectangular prism.</param>
/// <param name="height">The height of the rectangular prism.</param>
/// <param name="direction">The direction / angle to the rectangular prism will be drawn.</param>
/// <returns></returns>
public Bitmap DrawRectangularPrism(Graphics g, Color frontColor, Color leftRightColor, Color topBottomColor, int width, int height, Direction3D direction)
{
//The new width of the image
int newWidth = width + (width / 2);
//The new height of the image
int newHeight = height + (height / 2);
//Create a new bitmap with the new dimensions
Bitmap bitmap = new Bitmap (newWidth, newHeight, g );
//Create a new instance of the Graphics class from our new bitmap
Graphics graphics = Graphics.FromImage(bitmap);
//Ensure that we use high quality
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.High;
graphics.PageUnit = GraphicsUnit.Pixel;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
//A rectangle that represents the back of the rectangular prism
Rectangle back = new Rectangle ();
//A rectangle that represents the front of the rectangular prism
Rectangle front = new Rectangle ();
//Points that represent the top / bottom (depending on Direction3D) of the rectangular prism
Point [] top = new Point [] { };
//Points that represent the right / left side (depending on Direction3D) of the rectangular prism
Point [] right = new Point [] { };
//Assign the proper values to back, front, top, and right depending on the direction chosen
switch (direction)
{
//Assign values to make the angled towards the lower left
case Direction3D.DiagonalDownLeft:
{
back = new Rectangle (width / 2, 1, width, height );
front = new Rectangle (1, height / 2, width, height );
top = new Point [] { new Point (1, height / 2), new Point (width / 2, 1), new Point (width + (width / 2), 1), new Point (width, height / 2) };
right = new Point [] { new Point (width, height / 2), new Point (width + (width / 2), 1), new Point (width + (width / 2), height ), new Point (width, height + (height / 2)) };
break;
}
//Assign values to make the angled towards the lower right
case Direction3D.DiagonalDownRight:
{
back = new Rectangle (1, 1, width, height );
front = new Rectangle (width / 2, height / 2, width, height );
top = new Point [] { new Point ((width / 2), height / 2), new Point (1, 1), new Point (width, 1), new Point (width + (width / 2), height / 2) };
right = new Point [] { new Point (1, height ), new Point (1, 1), new Point (width / 2, height / 2), new Point (width / 2, height + (height / 2)) };
break;
}
//Assign values to make the angled towards the upper left
case Direction3D.DiagonalUpLeft:
{
back = new Rectangle (width / 2, height / 2, width, height );
front = new Rectangle (1, 1, width, height );
top = new Point [] { new Point (width / 2, height + (height / 2)), new Point (1, height ), new Point (width, height ), new Point (width + (width / 2), height + (height / 2)) };
right = new Point [] { new Point (width, height ), new Point (width, 1), new Point (width + (width / 2), height / 2), new Point (width + (width / 2), height + (height / 2)) };
break;
}
//Assign values to make the angled towards the upper right
case Direction3D.DiagonalUpRight:
{
back = new Rectangle (1, height / 2, width, height );
front = new Rectangle (width / 2, 1, width, height );
top = new Point [] { new Point (1, height + (height / 2)), new Point (width / 2, height ), new Point (width + (width / 2), height ), new Point (width, height + (height / 2)) };
right = new Point [] { new Point (1, height + (height / 2)), new Point (1, height / 2), new Point (width / 2, 1), new Point (width / 2, height ) };
break;
}
}
//Fill the back with black(this will not be seen)
graphics. FillRectangle(new SolidBrush (Color. Black), back );
//Fill the front with our frontColor
graphics. FillRectangle(new SolidBrush (frontColor ), front );
//Fill the top / bottom (depending on Direction3D) with our topBottomColor
graphics. FillPolygon(new SolidBrush (topBottomColor ), top );
//Fill the left / right (depending on Direction3D) with our leftRightColor
graphics. FillPolygon(new SolidBrush (leftRightColor ), right );
//Free up some resources
graphics.Dispose();
//Return the bitmap
return bitmap;
}
Copy & Paste
|