Connect with Expertise | Find Experts, Get Answers, Share Insights

c# house plan drawing

 
Join Date: Dec 2009
Posts: 15
#1: Dec 23 '09
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...

Expand|Select|Wrap|Line Numbers
  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Drawing.Drawing2D;
  7. namespace SalesGuruWizardInterface
  8. {
  9.     public partial class graphics : Form
  10.     {
  11.         public graphics()
  12.         {
  13.  
  14.         }
  15.         public int startpositionx = 190;
  16.         public int startpositiony = 640;
  17.         public int currentpositionx = 190;
  18.         public int currentpositiony = 640;
  19.         public int listx = 0;
  20.         public int listy = 0;
  21.         public int clistx = 0;
  22.         public int clisty = 0;
  23.         public List<int> xlist = new List<int>();
  24.         public  List<int> ylist = new List<int>();
  25.         public int count = 0;
  26.          public Graphics g;
  27.          bool shouldPaint = false;
  28.  
  29.         public void draw()
  30.         {
  31.              g = this.CreateGraphics();
  32.             Pen mypen = new Pen(Color.Brown,3);
  33.             Pen myredpen = new Pen(Color.Red, 10);
  34.             myrectangle = new Rectangle(currentpositionx , currentpositiony , 1, 1);
  35.             g.DrawRectangle(myredpen,myrectangle);
  36.             g.DrawLine(mypen, startpositionx , startpositiony ,      currentpositionx ,currentpositiony );
  37.             startpositionx = currentpositionx ;
  38.             startpositiony = currentpositiony ;
  39.             xlist.Add(startpositionx);
  40.             ylist.Add(startpositiony);
  41.             listBox1.Items.Add(xlist[count] + " " + ylist[count]);
  42.             count += 1;
  43.  
  44.  
  45.               }
  46.  
  47.  
  48.         public void Area(int[] x, int[] y,int intcount)
  49.         {
  50.  
  51.             {//This Construcot for Area of a Polygon
  52.                 double area = 0;
  53.  
  54.                 for (int i = 0; i < intcount; i++)
  55.                 {
  56.                     //MessageBox.Show(x[i] + " " + y[i] );
  57.  
  58.                     area += ((x[i] * y[i + 1]) - (x[i + 1] * y[i]));
  59.                 }
  60.                 area = area / 2;
  61.  
  62.  
  63.                 MessageBox.Show(((area* 4756.242568370986920332936979786)/1000000).ToString()  + "m squared");
  64.             }
  65.  
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.         private void upbutton_Click(object sender, EventArgs e)
  72.         {
  73.  
  74.                 currentpositiony -= Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
  75.                 draw();
  76.  
  77.  
  78.         }
  79.  
  80.         private void rightbutton_Click(object sender, EventArgs e)
  81.         {
  82.               currentpositionx += Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
  83.                 draw();
  84.  
  85.         }
  86.  
  87.         private void leftbutton_Click_1(object sender, EventArgs e)
  88.         {
  89.             currentpositionx -= Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
  90.             draw();
  91.         }
  92.  
  93.         private void downbutton_Click(object sender, EventArgs e)
  94.         {
  95.             currentpositiony += Convert.ToInt16((double.Parse(textBox1.Text) * 0.0145));
  96.             draw();
  97.         }
  98.  
  99.         private void uprightbutton_Click(object sender, EventArgs e)
  100.         {
  101.             currentpositiony -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
  102.             currentpositionx += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
  103.             draw();
  104.         }
  105.  
  106.         private void downrightbutton_Click(object sender, EventArgs e)
  107.         {
  108.             currentpositiony += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
  109.             currentpositionx -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
  110.            draw();
  111.         }
  112.  
  113.         private void downleftbutton_Click(object sender, EventArgs e)
  114.         {
  115.             currentpositiony += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
  116.             currentpositionx += Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
  117.             draw();
  118.         }
  119.  
  120.         private void upleftbutton_Click(object sender, EventArgs e)
  121.         {
  122.             currentpositiony -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Sin(45));
  123.             currentpositionx -= Convert.ToInt16(double.Parse(textBox1.Text) * 0.0145 * System.Math.Cos(45));
  124.             draw();
  125.         }
  126.  
  127.         private void Areabutton_Click(object sender, EventArgs e)
  128.         {
  129.             xlist.Add(xlist[0]);
  130.             ylist.Add(ylist[0]);
  131.             Area(xlist.ToArray(), ylist.ToArray(),count);
  132.         }
  133.  
  134.  
  135.         private void graphics_Load(object sender, EventArgs e)
  136.         {
  137.  
  138.             loadsg();
  139.  
  140.         }
  141.         public void loadsg()
  142.         {
  143. //i am using this as the canvas window
  144.             Graphics h = this.CreateGraphics();
  145.            Pen mypen = new Pen(Color.Blue, 5);
  146.            myrectangle = new Rectangle(180, 50, 800, 600);
  147.            h.DrawRectangle(mypen, myrectangle);
  148.  
  149.  
  150.         }
  151.  
  152.         private void cmdclear_Click(object sender, EventArgs e)
  153.         {
  154.  
  155.             g.Clear(Color.LightGray);
  156.             loadsg();
  157.           startpositionx = 190;
  158.           startpositiony = 640;
  159.           currentpositionx = 190;
  160.           currentpositiony = 640;
  161.          xlist.Clear();
  162.          ylist.Clear();
  163.          count = 0;
  164.         }

tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#2: Dec 23 '09

re: c# house plan drawing


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.
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#3: Dec 23 '09

re: c# house plan drawing


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.
 
Join Date: Dec 2009
Posts: 15
#4: Dec 23 '09

re: c# house plan drawing


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?
 
Join Date: Dec 2009
Posts: 15
#5: Dec 23 '09

re: c# house plan drawing


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


Expand|Select|Wrap|Line Numbers
  1.     class wall
  2.     {
  3.         public double thickness = 0.0;
  4.         public int[] startpoint = { 0, 0 };
  5.         public int[] currentpoint = { 0, 0 };
  6.         public double walllength = 0;
  7.         public string walltype = "Single";
  8.         public Graphics wallgraphic;
  9.         public Pen wallpen;
  10.         //drawing the wall
  11.         public void drawwall(int xstart,int ystart,int  length,string wtype, string direction)
  12.         {
  13.             switch (direction)
  14.             {
  15.                 case "Right":
  16.                     {
  17.                         wallpen = new Pen(Color.Red, 4);
  18.                         wallgraphic.DrawLine(wallpen, xstart, ystart, xstart + length, ystart);
  19.                         break;
  20.                     }
  21.                default:
  22.                     {
  23.                         break;
  24.                     }
  25.            }
  26.         }
  27.  
  28.     }
  29. }
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#6: Dec 23 '09

re: c# house plan drawing


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.
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#7: Dec 23 '09

re: c# house plan drawing


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

Expand|Select|Wrap|Line Numbers
  1. public wall(Color color, Point start, Point end, String Walltype)
  2. {
  3.    WallColor = color;
  4.    StrartPoint = start;
  5.    EndPoint = end;
  6.    WallType = Walltype;
  7. }
Now you can make a
Expand|Select|Wrap|Line Numbers
  1. wall MyWall = new Wall(Color.Red, Point(4,10), Point(60,50), "Interior");
Reply