Kira
Kira

Reputation: 1443

Drawing cube from rectangle in C#

I have the co-ordinates of a rectangle. How can I draw a cube with this in C#?

graphics.drawRectangle(Pens.Black, x, y, width, height);

Rectangle has four co-ordinates but a cube has 8 co-ordinates. How can I locate the other four points of the rectangle to make it look like a cube.

When drawing a cube by hand from a rectangle, we need three-dimensions length X width X height. We will use an angle of 60 to 70 degree (approx) from X axis to draw z-axis and it will look like 3D. Similarly, we can draw length dimension for the rectangle and locate the point (x',y') for second rectangle. We will use the same height and width for the second rectangle and joining the two rectangles will give the cube.

I'm trying to do this in C#. To do this I need a co-ordinate of the second rectangle which is at an arbitrary distance (say 50) at an angle of 70 degree (approx) from (x, y) of first rectangle

Upvotes: 0

Views: 4130

Answers (2)

Weyland Yutani
Weyland Yutani

Reputation: 4960

Here's a cheap hack for a filled cube (not wireframe), you can modify the way the 'i' part works to change the "perspective"

//draw the sides of the cube black
for (int i=-10;x<0;i++)
     graphics.drawRectangle(Pens.Black, x+i, y+i, width, height);
//draw the front of the cube red
graphics.fillRectangle(Brushes.Red, x, y, width, height);

Upvotes: 1

Emond
Emond

Reputation: 50692

There are many ways to draw a cube but a rectangle will always be a rectangle. You cannot add points to it (it would not be a rectangle anymore).

Have a look at this article It shows you one way to draw a cube.

If you are willing to have a less sophisticated way you might transform (skew) 2 or 3 rectangles and turn them into a projection of a cube.

Upvotes: 1

Related Questions