Graphing in PictureBox

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 3
Reputation: metkillerjoe is an unknown quantity at this point 
Solved Threads: 0
metkillerjoe metkillerjoe is offline Offline
Newbie Poster

Graphing in PictureBox

 
0
  #1
Mar 17th, 2007
I have been trying to get a simple linear equation to draw. I can draw the axis fine but the equation will not draw. I think one would need a save/refresh function(s), but I don't know how to go about doing this:

  1. pw = pictureBox1.Size.Width;
  2. ph = pictureBox1.Size.Height;
  3. w = pictureBox1.Size.Width / 2;
  4. h = pictureBox1.Size.Height / 2;
  5. Graphics objGraphics = this.pictureBox1.CreateGraphics();
  6. Pen pen = new Pen(Color.Black);
  7. b = 2;
  8. m = 2;
  9. for (x = 0; x < 426; x++)
  10. {
  11. y = m * x + b;
  12. x = (y - b) / m;
  13. objGraphics.DrawLine(pen, x, y, x, y);
  14. System.Drawing.Drawing2D.GraphicsState graph = objGraphics.Save();
  15. objGraphics.Restore(graph);
  16. }
  17.  
  18.  
  19.  
  20. objGraphics.DrawLine(pen, 0, h, pw, h);
  21. objGraphics.DrawLine(pen, w, 0, w, ph);
Thanks for any help. I should mention that I did declare all variables (x, y, m, and b). I'm just starting out with VC#, so that might explain the sloppy or simple code.
Last edited by metkillerjoe; Mar 17th, 2007 at 2:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: DDoSAttack is an unknown quantity at this point 
Solved Threads: 2
DDoSAttack's Avatar
DDoSAttack DDoSAttack is offline Offline
Newbie Poster

Re: Graphing in PictureBox

 
0
  #2
Mar 18th, 2007
That is because you are plotting a point right on top of the original point.

I changed this line...
  1. objGraphic.DrawLine(pen, x, y, -x, -y);
Here is the code I used...
  1. int picBoxWidth = pictureBox1.Size.Width;
  2. int picBoxHeight = pictureBox1.Size.Height;
  3. int halfWidth = pictureBox1.Size.Width / 2;
  4. int halfHeight = pictureBox1.Size.Height / 2;
  5.  
  6. Graphics objGraphic = this.pictureBox1.CreateGraphics();
  7.  
  8. Pen pen = new Pen(Color.Black);
  9.  
  10. int b = 2;
  11. int m = 2;
  12. for (int x = 0; x < 426; x++)
  13. {
  14. int y = m * x + b;
  15. x = (y - b) / m;
  16. objGraphic.DrawLine(pen, x, y, -x, -y);
  17. System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
  18. objGraphic.Restore(graph);
  19. }
  20.  
  21. objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
  22. objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
Hope that helps
Last edited by DDoSAttack; Mar 18th, 2007 at 3:37 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: metkillerjoe is an unknown quantity at this point 
Solved Threads: 0
metkillerjoe metkillerjoe is offline Offline
Newbie Poster

Re: Graphing in PictureBox

 
0
  #3
Mar 19th, 2007
Originally Posted by DDoSAttack View Post
That is because you are plotting a point right on top of the original point.

I changed this line...
  1. objGraphic.DrawLine(pen, x, y, -x, -y);
Here is the code I used...
  1. int picBoxWidth = pictureBox1.Size.Width;
  2. int picBoxHeight = pictureBox1.Size.Height;
  3. int halfWidth = pictureBox1.Size.Width / 2;
  4. int halfHeight = pictureBox1.Size.Height / 2;
  5.  
  6. Graphics objGraphic = this.pictureBox1.CreateGraphics();
  7.  
  8. Pen pen = new Pen(Color.Black);
  9.  
  10. int b = 2;
  11. int m = 2;
  12. for (int x = 0; x < 426; x++)
  13. {
  14. int y = m * x + b;
  15. x = (y - b) / m;
  16. objGraphic.DrawLine(pen, x, y, -x, -y);
  17. System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
  18. objGraphic.Restore(graph);
  19. }
  20.  
  21. objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
  22. objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
Hope that helps
Thanks that did help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC