### How to draw a cube?

I do my homework test, it puzzles me:
 A cube is made up of twelve lines. Write a function that draws a cube to the client area of a window.

I was stuck the coordinate, how can I define the Cube class member, how can I describe the cooridinate, use POINT member or Line(user defined type contains 2 POINT)?

can you give me some help about how to handle the coordinate?
Are you drawing in 3D or 2D? This is not clear from your original post. Normally a cube would be defined in 3 dimensions although it may be you are expected to have a "2D drawing" of a cube made to the client area, using 2D line segments.

If it's for 2D, you could represent this as 12 distinct lines (not the best way to represent it), or you could draw a square of a given edge length, then draw a second square of the same size but with offset position in x and y, and then connect the 4 points representing each of the squares one-to-one. So for a square where you have 4 2D points defined as (1,1) , (1,5) , (5,1) , (5,5) , you could draw another square offset by e.g. 2 in x and 1 in y, giving you points: (3,2), (3,6), (7,2), (7,6). And then connect one-to-one with 4 more lines to assure your edges for the side faces will be properly drawn between the front and back face: (1,1) -> (3,2) , (1,5) -> (3,6) , (5,1) -> (7,2) , (5,5) -> (7,6). Like I said, this is a 2D drawing though, not a 2D projection, so 3D perspective is not taken properly into account.

If it's for 3D, representing it as 8 3D points would make it more liable for the edges to be drawn incorrectly, connecting points that do not lie on the same plane. If you make sure you connect them in proper order, this representation could work, as could representing them in your class as 12 3D line segments.

For greater efficiency (of allocating memory), in 3D you could represent a cube using one point and it's edge length (and then calculate the remaining points by adding the edge length to each of the dimensions of the 3D point you already have specified -- so you could generate the other 7 points from the single point you specify and the edge length).

e.g.
 ``123456`` ``````class cube_1 { public: float point[3]; // (x,y,z) float edgeLength; };``````

So, if you're doing a 2D drawing in this case, 8 points connected with lines in proper order, 12 explicitly defined lines(using 2 2D points each), or defining a square and offsetting it (and then connecting the points of the two squares one-to-one) could all give you the required result.
Last edited on
I want to draw a cube in 2D. Just use #include <windows.h>, can you give me some code how to handle the coordinate, I found it's hard to do, I think only 3d coordinate can describe it, how can 3d describe?
In 3D you'd use an API like DirectX or OpenGL, which has similar point-drawing, line-drawing functions (polygon-drawing functions etc), that take parameters in 3 dimensions (x,y,z). But I mentioned that because I didn't know if you refer to 2D or 3D.

Since you want to draw in 2D, you could use the GDI functions of the Windows API. You can represent a cube in 2D as a number of 2D line segments (12 if you make it transparent). I'm not very familiar with GDI functions at present, although you could use `MoveToEx()`, and then `Rectangle()` for making two squares and `LineTo()` to connect the side edges, getting something like a "wireframe view" of the cube in 2D:

http://pballew.net/cube.jpg

You could use exclusively `LineTo()` and no `Rectangle()`, or use `Polyline()` etc, these can create equivalent results and it's up to how you want to do it.

Look up the definition of these functions in MSDN and also check out a tutorial on basic GDI drawing, like the following:

http://www.functionx.com/win32/Lesson11.htm

The 2D origin (0,0) is placed in the top left of the window in GDI, so the coordinates you specify would have +X moving from the left towards the right side of your window and +Y moving from the top towards the bottom side.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````/* A line is a junction of two points. This means that a line has a beginning and an end. The beginning and the end are two distinct points . In real life, before drawing, you should define where you would start. To help with this, you can use MoveToEx() function. Its syntax is: BOOL MoveToEx(HDC hdc, int X, int Y, LPPOINT lpPoint); The origin of a drawing is specified as the (X, Y) point. To end the line, you use the LineTo() function. Its syntax is: BOOL LineTo(HDC hdc, int nXEnd, int nYEnd); The end of a line can be defined by its horizontal (nXEnd) and its vertical measures (nYEnd). Here is an example that draws a line starting at a point defined as (10, 22) coordinates and ending at (155, 64): */ //--------------------------------------------------------------------------- LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT Ps; switch(Msg) { case WM_PAINT: hDC = BeginPaint(hWnd, &Ps); MoveToEx(hDC, 60, 20, NULL); LineTo(hDC, 264, 122); EndPaint(hWnd, &Ps); break; case WM_DESTROY: PostQuitMessage(WM_QUIT); break; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; } //--------------------------------------------------------------------------- ``````

Hope this helps,

Ogoyant

Last edited on
sorry, I don't get this, I know how to draw a Line, but I found it's difficult to draw a cube, because the coordinate it hard, I think only 3d coordinate can describe this, can you give me some example about how to draw a cube?
Normally, a cube would certainly need 3 dimensions to be defined as it is a 3-dimensional shape. However, you can "draw" it in 2D without actually describing it in it's 3 dimensions -- much like you could draw one on a flat piece of paper.

Like I said the GDI functions are not familiar to me. However, building on the simple line example in the tutorial that I referred to previously, I think the cube composed of 12 line segments could be drawn using 12 LineTo() commands in the following way (maybe this example cube will be a bit small though):

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````//--------------------------------------------------------------------------- LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT Ps; switch(Msg) { case WM_PAINT: hDC = BeginPaint(hWnd, &Ps); //--- MoveToEx(hDC, 60, 20, NULL); LineTo(hDC, 90, 20); LineTo(hDC, 90, 50); LineTo(hDC, 60, 50); LineTo(hDC, 60, 20); MoveToEx(hDC, 80, 40, NULL); LineTo(hDC, 110, 40); LineTo(hDC, 110, 70); LineTo(hDC, 80, 70); LineTo(hDC, 80, 40); MoveToEx(hDC, 60, 20, NULL); LineTo(hDC, 80, 40); MoveToEx(hDC, 90, 20, NULL); LineTo(hDC, 110, 40); MoveToEx(hDC, 60, 50, NULL); LineTo(hDC, 80, 70); MoveToEx(hDC, 90, 50, NULL); LineTo(hDC, 110, 70); //--- EndPaint(hWnd, &Ps); break; case WM_DESTROY: PostQuitMessage(WM_QUIT); break; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; } //--------------------------------------------------------------------------- ``````

I haven't tested this but you should probably be able to test it relatively fast since you're familiar with GDI. Let me know if it works.
Last edited on
Topic archived. No new replies allowed.