|
|
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » How to implent a rubber band Class in u++
Re: How to implent a rubber band Class in u++ [message #17373 is a reply to message #17298] |
Sat, 09 August 2008 18:43 ![Go to previous message Go to previous message](theme/default/images/up.png) |
|
mrjt wrote on Wed, 06 August 2008 12:04 | I can think of two possible optimisations (besides PolyLine, which is definitely the first one to use)
1- Avoid adding adjacent collinear points. In practice this is a very minor optimisation except for special cases, but it appeals to my sense of correctness.
.....
2- Use a back buffer. Providing you have a static background (one that will not change while the rubber band is being drawn), you can use a back buffer to cache the background + the current rubber band and only draw the last line.
This was a major improvement for me, eliminating flickering entirely.
.........
|
Very Hard optimization. Thank you!
Bu in this example is an error!
void RubberBand::AddPoint(const Point &newp)
{
if (points.GetCount() < 2)
return points.Add(newp);
// Get line vectors
const Point &p = points[points.GetCount()-2];
Point p1 = points.Top() - p;
Point p2 = newp - p;
// If gradient is different, add the new point
if (p1.x*p2.y - p2.x*p1.y)
points.Add(newp);
// Otherwise update last points
else
==>HERE points.Top() = newp;
}
The bug is when the newpoint is in the line draws from the last 2 points AND the newpoint is in between from the last 2 points!
Corrected code is:
void RubberBandClass::AddPoint(const Point &newp) {
if (points.GetCount() < 2)
return points.Add(newp);
// Get line vectors
const Point &p_second_last = points[points.GetCount()-2];
const Point &p_last = points.Top();
Point p1 = p_last - p_second_last;
Point p2 = newp - p_second_last;
// If gradient is different, add the new point
if (p1.x*p2.y - p2.x*p1.y)
points.Add(newp);
// Otherwise update last points if the newpoint is continue of the last point from the second last point
else{
int pos_from_last = 0;
int pos_1=newp.x, pos_2=p_last.x, pos_3=p_second_last.x;
if (!((p_last.x == p_second_last.x)&&(newp.x == p_last.x)))
int pos_1=newp.y, pos_2=p_last.y, pos_3=p_second_last.y;
if (pos_2 > pos_3 ){
if (pos_1 > pos_2) {
pos_from_last = 1;
}
else if ( pos_1 < pos_3) {
pos_from_last = 2;
}
}
else {
if (pos_1 < pos_3) {
pos_from_last = 1;
}
else if (pos_1 >pos_2){
pos_from_last = 2;
}
}
if (pos_from_last) points[points.GetCount()-pos_from_last] = newp;
}
}
In base of this example I can write paint method of controls more optimized!
Thanks a lot about back buffer (BackDraw)!
Is other ideas?
[Updated on: Sat, 09 August 2008 18:51] Report message to a moderator
|
|
|
![Read Message Read Message](theme/default/images/read.png) |
|
How to implent a rubber band Class in u++
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mrjt on Thu, 25 October 2007 16:44
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 07 July 2008 18:35
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Fri, 25 July 2008 20:02
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Sat, 26 July 2008 12:03
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 26 July 2008 16:18
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Sat, 26 July 2008 16:49
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 26 July 2008 22:45
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sun, 27 July 2008 01:02
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Fri, 01 August 2008 08:53
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Fri, 01 August 2008 22:33
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 02 August 2008 09:49
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 04 August 2008 18:56
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Mon, 04 August 2008 19:39
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 04 August 2008 22:33
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Tue, 05 August 2008 15:44
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Tue, 05 August 2008 17:37
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Tue, 05 August 2008 19:54
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Wed, 06 August 2008 00:29
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Wed, 06 August 2008 08:34
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mrjt on Wed, 06 August 2008 11:04
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 09 August 2008 18:43
|
Goto Forum:
Current Time: Mon Jun 17 06:44:15 CEST 2024
Total time taken to generate the page: 0.02097 seconds
|
|
|