c# house plan drawing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charindal
    New Member
    • Dec 2009
    • 17

    c# house plan drawing

    i am trying to create a program that is able to draw a housing plan. the user should be able to input the required meters then upon clicking a left,right,up, down button, the system draws a line in that direction. I have managed to do this but however how do i protect my painted window when it has been obscured by another window. i also would like to be able to click on corners and be able to draw interior walls from a point marked from a corner. here is my code for what i have done so far...

    Code:
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    namespace SalesGuruWizardInterface
    {
        public partial class graphics : Form
        {
            public graphics()
            {
                      
            }
            public int startpositionx = 190;
            public int startpositiony = 640;
            public int currentpositionx = 190;
            public int currentpositiony = 640;
            public int listx = 0;
            public int listy = 0;
            public int clistx = 0;
            public int clisty = 0;
            public List<int> xlist = new List<int>();
            public  List<int> ylist = new List<int>();
            public int count = 0;
             public Graphics g;
             bool shouldPaint = false;
            
            public void draw()
            {
                 g = this.CreateGraphics();
                Pen mypen = new Pen(Color.Brown,3);
                Pen myredpen = new Pen(Color.Red, 10);
                myrectangle = new Rectangle(currentpositionx , currentpositiony , 1, 1);
                g.DrawRectangle(myredpen,myrectangle);
                g.DrawLine(mypen, startpositionx , startpositiony ,      currentpositionx ,currentpositiony );
                startpositionx = currentpositionx ;
                startpositiony = currentpositiony ;
                xlist.Add(startpositionx);
                ylist.Add(startpositiony);
                listBox1.Items.Add(xlist[count] + " " + ylist[count]);
                count += 1;
               
                
                  }
           
    
            public void Area(int[] x, int[] y,int intcount)
            {
                
                {//This Construcot for Area of a Polygon
                    double area = 0;
                    
                    for (int i = 0; i < intcount; i++)
                    {
                        //MessageBox.Show(x[i] + " " + y[i] );
                       
                        area += ((x[i] * y[i + 1]) - (x[i + 1] * y[i]));
                    }
                    area = area / 2;
                   
                    
                    MessageBox.Show(((area* 4756.242568370986920332936979786)/1000000).ToString()  + "m squared");
                }
    
                
            }
            
    
           
            private void upbutton_Click(object sender, EventArgs e)
            {
                
                    currentpositiony -= Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
                    draw();
                
                
            }
    
            private void rightbutton_Click(object sender, EventArgs e)
            {
                  currentpositionx += Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
                    draw();
                
            }
    
            private void leftbutton_Click_1(object sender, EventArgs e)
            {
                currentpositionx -= Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
                draw();
            }
    
            private void downbutton_Click(object sender, EventArgs e)
            {
                currentpositiony += Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
                draw();
            }
    
            private void uprightbutton_Click(object sender, EventArgs e)
            {
                currentpositiony -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
                currentpositionx += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
                draw();
            }
    
            private void downrightbutton_Click(object sender, EventArgs e)
            {
                currentpositiony += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
                currentpositionx -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
               draw();
            }
    
            private void downleftbutton_Click(object sender, EventArgs e)
            {
                currentpositiony += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
                currentpositionx += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
                draw();
            }
    
            private void upleftbutton_Click(object sender, EventArgs e)
            {
                currentpositiony -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
                currentpositionx -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
                draw();
            }
    
            private void Areabutton_Click(object sender, EventArgs e)
            {
                xlist.Add(xlist[0]);
                ylist.Add(ylist[0]);
                Area(xlist.ToArray(), ylist.ToArray(),count);
            }
    
           
            private void graphics_Load(object sender, EventArgs e)
            {
    
                loadsg();
                
            }
            public void loadsg()
            {
    //i am using this as the canvas window
                Graphics h = this.CreateGraphics();
               Pen mypen = new Pen(Color.Blue, 5);
               myrectangle = new Rectangle(180, 50, 800, 600);
               h.DrawRectangle(mypen, myrectangle);
                
                
            }
    
            private void cmdclear_Click(object sender, EventArgs e)
            {
               
                g.Clear(Color.LightGray);
                loadsg();
              startpositionx = 190;
              startpositiony = 640;
              currentpositionx = 190;
              currentpositiony = 640;
             xlist.Clear();
             ylist.Clear();
             count = 0;
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Tip: The Lists of x and y... You might want to look at the Point object to replace all that. A point has an x and y property. Its the way standard geometry works. Two points determine a line and so on.
      Your startpoint would then be new point(190, 640)

      how do i protect my painted window when it has been obscured by another window.
      I don't understand the question. What do you mean by "protect the window" ?

      Oh... wait... you might want to look at overriding the "ONPAINT" method of the form and put your drawing functionality in that. This way your drawing will take place every time the form is redrawn, like after a portion has been obstructed.

      i also would like to be able to click on corners and be able to draw interior walls from a point marked from a corner.
      A wall would be a line... which is two points.
      So draw a line from the mouse coordinates of click '1' to click '2'

      But I think you have a more fundamental issue with your program. Once anything is painted on your canvas, it's there for life.
      You don't seem to be keeping a collection of objects, such as walls. How do you plan on letting your user make changes?
      You might want to consider a 'wall' class with properties like points, thickness, pen.
      Then make a List<wall>. Once you have a list of walls you can move one by changing its coordinates, then you redraw.

      A door class might have properties like the points, and degrees of rotation
      Then you have a list of doors.

      Your drawing function then draws all your walls in the wall list, all your doors in the door list, etc.

      Comment

      • charindal
        New Member
        • Dec 2009
        • 17

        #4
        Thank you for the advice. i really am getting the picture of wht you are saying. is it possible if you can just write me up a sample class with walls and doors, windows and one example for drawing under the override onpaint?

        Comment

        • charindal
          New Member
          • Dec 2009
          • 17

          #5
          i have tried to implement a class like the following please correct me if am wrong and suggest any advice


          Code:
              class wall
              {
                  public double thickness = 0.0;
                  public int[] startpoint = { 0, 0 };
                  public int[] currentpoint = { 0, 0 };
                  public double walllength = 0;
                  public string walltype = "Single";
                  public Graphics wallgraphic;
                  public Pen wallpen;
                  //drawing the wall
                  public void drawwall(int xstart,int ystart,int  length,string wtype, string direction)
                  {
                      switch (direction)
                      {
                          case "Right":
                              {
                                  wallpen = new Pen(Color.Red, 4);
                                  wallgraphic.DrawLine(wallpen, xstart, ystart, xstart + length, ystart);
                                  break;
                              }
                         default:
                              {
                                  break;
                              }
                     }
                  }
          
              }
          }
          Last edited by tlhintoq; Dec 23 '09, 05:59 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Please use code tags as previously mentioned. Its part of the posting guidelines in order to make it easier to read. It helps those that are helping you. If it isn't important enough to make it easier on those help you, then it show its isn't important enough to bother reading.

            TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              I'm not so sure that having the drawing routine inside the wall class is the best plan.

              Imagine a time 6 months from now when your user will want to be able to zoom in and out to see just one room in greater detail. If your wall class does its own drawing, and the window class does its own etc. you have a lot of code to change. But if the classes only have data such as their pen style and coordinates, used by a common draw routine in the main program... then the main program can calculate a scale effect on those coordinates.

              You may also want to consider a constructor to make life easier on yourself. Like this you have to make a new wall object, then set all its values. You could do it all in one step

              Code:
              public wall(Color color, Point start, Point end, String Walltype)
              {
                 WallColor = color;
                 StrartPoint = start;
                 EndPoint = end;
                 WallType = Walltype;
              }
              Now you can make a
              Code:
              wall MyWall = new Wall(Color.Red, Point(4,10), Point(60,50), "Interior");

              Comment

              Working...