I’m developing CAD like application in C# (Forms, no external framework). I have implemented pan, zoom, select; but it’s very slow, because on every mouse move it redraw all lines.
How to build app to be faster and more efficient? How to redraw just one line?
code for drawing (on standard Panel):
void pan_Paint(object sender, PaintEventArgs e)
{
foreach (Entitie o in sketch.Entities)
{
if (o is Line)
{
Line l = (Line)o;
Point p1 = pointScreen(l.P1.X.CurrentValue, l.P1.Y.CurrentValue);
Point p2 = pointScreen(l.P2.X.CurrentValue, l.P2.Y.CurrentValue);
e.Graphics.DrawLine(Pens.Black, p1, p2);
}
}
}
for drawing new line:
void pan_MouseMove(object sender, MouseEventArgs e)
{
if (tmpEntitie != null)
{
Line l = (Line)tmpEntitie;
l.P2 = e.Location;
pan.Invalidate();
}
}
I think you need to use a space partitioning technique which divide the space in regions or quadrants and redraw the objects only if they are in the region showing in the current viewport.
Here are some links to start:
http://en.wikipedia.org/wiki/Space_partitioning
http://en.wikipedia.org/wiki/Quadtree
Libraries:
http://www.vividsolutions.com/jts/jtshome.htm (Java Topology Suite)
http://code.google.com/p/nettopologysuite/ (JTS in C#)