hi,
actually i wanted to draw two cubes programmatically in C# with only top,front and right portions visible.the top and right portion will have the same dimensions but depending on the parameter width provided by the user,the front portion will increase along the horizontal axis.One cube will increase along the positive x axis and other one will increase towards the negative x axis i.e one will progress towards the left side and other will progress towards the right side.
Can you please provide the C# code for this asap.Thanks in advance

Recommended Answers

All 21 Replies

Actually i want a cube which increases along only one axis i.e. x axis and remains constant along the other axis.Plz help.its really urgent.

Perhaps something like this:

namespace Cube
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // draw in the form
            Draw(e.Graphics, 100); // a cube of 100 pixs wide
        }

        public void Draw(Graphics G, int width)
        {
            int height = 50; // height of the cube (y-axis)
            int skew = 20;
            Point Org = new Point(100, 100);
            Pen pencil = new Pen(Color.Blue, 1f);
            Rectangle R = new Rectangle(Org.X, Org.Y, width, height);
            G.DrawRectangle(pencil, R);
            G.DrawLine(pencil, Org.X, Org.Y, Org.X + skew, Org.Y - skew);
            G.DrawLine(pencil, Org.X + skew, Org.Y - skew, Org.X + width + skew, Org.Y - skew);
            // continue with DrawLine here to finish it
        }
    }
}

Start a new forms project, install a Paint event and fill in the above code.
Success!

How to fill the top and right portion of the cube formed by lines.Plz help.

Would it not be fun to try that for yourself?
For the above code I did the coordinates in my head, but if it gets more complicated I use some squared sheet and draw on it to find out the coordinates.
Just give it a try, it's like swimming, you're afraid to start it, but once you get used to it, it's fun!
:)

well i was able to write the code for the rest of the lines.but not sure about the how to fill the area bounded by the lines.

I wuld have loved to do the entire thing on my own but have to release to QA tmrw and
it's still not done..:(

g.DrawLine(pencil, Org.X + wd + skew, Org.Y - skew, Org.X + wd, Org.Y);
g.DrawLine(pencil, Org.X + wd + skew, Org.Y - skew, Org.X + wd + skew, Org.Y - skew + height);
g.DrawLine(pencil, Org.X + wd + skew, Org.Y - skew + height, Org.X + wd, Org.Y + height);

This is how i filled the rectangle

g.FillRectangle(new SolidBrush(Color), R);

Plz help

And the other issue is I have to draw two cubes from the same origin.One has to progress in the positive x -axis based on the width parameter passed.
And the other has to progress in the negative x-axis based on width parameter passed.
The top and right portion of the cube have to remain the same.

Hey i was able 2 fill the polygons created by those points using the FillPolygon() Method.But still stuck with this one..not able 2 figure this out.Please suggest the approach for this.

And the other issue is I have to draw two cubes from the same origin.One has to progress in the positive x -axis based on the width parameter passed.
And the other has to progress in the negative x-axis based on width parameter passed.
The top and right portion of the cube have to remain the same.

Sorry I posted the WPF version of the polygon class.
I post the my new code here, you just have to fill in the right side with a Polygon.

public void Draw(Graphics G, int width)
        {
            int height = 50; // height of the cube (y-axis)
            int skew = 20;
            Point Org = new Point(100, 100);
            Pen pencil = new Pen(Color.Blue, 1f);
            SolidBrush brush = new SolidBrush(Color.LightBlue);
            Rectangle R = new Rectangle(Org.X, Org.Y, width, height);
            Point[] topCube;
            topCube = new Point[]{
                new Point(Org.X, Org.Y),
                new Point(Org.X + skew, Org.Y - skew),
                new Point(Org.X + width + skew, Org.Y - skew),
                new Point(Org.X + width , Org.Y)};
            G.DrawPolygon(pencil, topCube);
            G.FillPolygon(brush, topCube);
            G.DrawRectangle(pencil, R);
            G.FillRectangle(new SolidBrush(Color.LightSkyBlue), R);
           
        }

Your other issue you could solve by using the a "negative width". Instead of adding it to the origin, subtract it! Calculate coordinates accordingly for rectangle and polygon
You only have to fill two sides here, because the left side is not visible.
Success!

Well please see the attached image.The four cube like structures that actually represent the values is what needs to be implemented.So i have added a user control representing this cubical structure and intend to reuse it everywhere.I hv created another user control which is in the form of a table layout panel with 2 rows and 2 columns where these 4 cubes will be added in each cell to implement this functionality.
The issue that am currently facing is that how to determine the exact position of the point from where these 4 cubes have to be drawn.
how to pass the x and y coordinates of the point to the code which you hv written.

plz help me.This functionality is actually driving me nuts...too exhausted 2 evn think nw..
:'(

Hey, great user control!
But a TableLayoutPanel is perhaps not the best way to proceed. Perhaps 4 panels?

Use 2 cubes, a negCube and a posCube.
I have given you code for a "posCube". Look at the Org Point. You could draw several "posCubes" on a form just by changing the coordinates of this Point. Experiment!
I think you are smart enough to figure out to do that for a cube with "negative width". :)

Hey thanks a lot.
But what i am wondering is that if we give the absolute position of the start point of these cubes through the X and Y coordinates of the Org Point,then based on the screen resolution and if we minimize/maximize screen,aren't these coordinates likely 2 change.

Please correct me if i am wrong.

I'm not THAT experienced in the matter but I think that if you have a form with a Panel and you draw in the Panel, nothing much would change.
Like an OK button, it would not change.
A Panel's coordinates start at top/left as 0/0.
I did my drawing in the Form to keep it simple, but of course that is not the way to go.
Happy programming!

This "negative width" concept is not working for me.Can you please check if it is working for you.

Have a look:

public void Draw(Graphics G, int width)
        {
            int height = 50; // height of the cube (y-axis)
            int skew = 20;
            Point Org = new Point(100, 100);
            Pen pencil = new Pen(Color.Blue, 1f);
            SolidBrush brush = new SolidBrush(Color.LightBlue);
            Rectangle R = new Rectangle(Org.X, Org.Y, width, height);
            Point[] topCube;
            topCube = new Point[]{
                new Point(Org.X, Org.Y),
                new Point(Org.X + skew, Org.Y - skew),
                new Point(Org.X + width + skew, Org.Y - skew),
                new Point(Org.X + width , Org.Y)};
            G.DrawPolygon(pencil, topCube);
            G.FillPolygon(brush, topCube);
            G.DrawRectangle(pencil, R);
            G.FillRectangle(new SolidBrush(Color.LightSkyBlue), R);
            // still to do:right side

            // do the negative
            Org.X = 80; //redefine origin
            width = 60; //the "negative" width (perhaps an offset is needed
            // depending on how much space you want between the 2 cubes)
            Rectangle N = new Rectangle(Org.X - width, Org.Y, width, height);
            G.DrawRectangle(pencil, N); //guess you can fill in the rest
           
        }

My other query is how to apply border around this top and right polygon which we made through the array of points.Plz help.

The pensize is 1f thick for the moment.
Just increase that floating point number to whatever size you want.

This is not working even if i give 5f or 10f or 100f.Please check.

Works with me.
You did use different colors for Fill and Pen?

Can anyone tell me how to draw a cube in console application?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.