943,148 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 661
  • C++ RSS
Feb 8th, 2010
0

Draw path with variables

Expand Post »
Hi there
I want to use c++ to draw a path
The path is based on two variables, x and y
and x and y will change automatically every second

So I think the flow should be like this:
t=0, (x,y)=(0,0) ->draw a point on the graph
t=1, (x,y)=(2,2)->draw another point on the graph
....
repeat the above steps until the user stop the program.

But I have no idea how can I ask the program to draw point every second and keep all the points in the graph? I tried to use a timer
C++ Syntax (Toggle Plain Text)
  1. private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
  2. { ShowGraphics=true; }
so when ShowGraphics is true,
C++ Syntax (Toggle Plain Text)
  1. if (IsShowGraphics==false)
  2. return;
  3.  
  4. Color Black=Color::FromArgb(250, Color::Black);
  5.  
  6. SolidBrush^ Brush_A = gcnew SolidBrush( Black );
  7.  
  8. if (x!=0 && y!=0)
  9. { CallerGDI->FillRectangle(Brush_A,x,y,10,10); //draw point
  10. IsShowGraphics=false;
  11. }

But the program only draws one point at each time AND replaces the previous one. How can I keep the "old" points in the graph?

Thanks for yr time, I really appreciate your help~
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yin1031 is offline Offline
2 posts
since Feb 2010
Feb 8th, 2010
0
Re: Draw path with variables
This is a frustrating thing to deal with. I looked at it as such:
Make a class that holds your data points in a list and has a member method Draw, drawing each point up until time.
C++ Syntax (Toggle Plain Text)
  1. void Points::Draw(Graphics^ g , int time)
  2. {
  3. Pen ^ mypen = gcnew Pen(Color::Black);
  4. for(int i = 0;i<time;i++)
  5. g->DrawRectangle(mypen,listpts[i].X,listpts[i].Y,1,1);
  6. }

In form1.h have 2 event handlers
C++ Syntax (Toggle Plain Text)
  1. private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
  2. pts->Draw(e->Graphics,time);
  3. time++;
  4.  
  5. }
  6. private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
  7. Invalidate();
  8. }

Use an interval on the timer of 1000 and start it in the Form1 constructor. Each time the timer ticks (1 sec) it will paint all of the dots up until that time. I'm sure there's a better way to do it but there may be no escaping the need to repaint all of them on each new dot.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 9th, 2010
0
Re: Draw path with variables
thank you jonsca, it works^^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yin1031 is offline Offline
2 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: while(true) loop + getline() = infinite loop??
Next Thread in C++ Forum Timeline: Is Visual C++ (ver8) Broken





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC