A
Andreas
Hi,
I was debating if I should be posting this in the drawing newsgroup but
finally decided that it wasn't drawing related (even if that's what I'm
ultimatly doing) but instead optimization / math. In a sense it's not
c-sharp specific either but I hope someone can share some suggestions. So
what I'm doing can be viewed as graphis a graph (Vertex and Edges).
I have a series of points drawn on a bitmap and some of them should be
connected with a line. That itself is no problem, it would be a basic
DrawLine call. However what I need to do is to have the line not start/end
in the center of each point but instead at a certain distance from it, i.e
having a sort of safe radius around each point
So instead of this (sorry for the poor ascii)
*----*
I want this
* ---- *
Now please note that this would not be a problem is all points where lined
up on the horizontal and vertical axis but they are not, meaning that I will
be drawing diagonale lines. So what I came up with was some basic
trigonomety to calculate where the line coordinates are. My problem is
performance and the need to optimize. I have about 5000 points and they are
all connected in a web. I have a need to redraw this when ever scrolling
occures (yeah I only redraw the things that are visible in my viewing pane)
and performance is a bit slow
So is there any math-tricks / optimizations anyone could recommend to do the
same? Below is the code I use to calculate and draw the line. For the sake
of the argument, assume I cannot cache the calculation after the first time
it's been drawn and have a need to run it each time
int safeRadius = 7;
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
double angleX = Math.Abs(p2.X - p1.X);
if (angleX == 0)
angleX = 1;
double angleY = Math.Abs(p2.Y - p1.Y);
if (angleY == 0)
angleY = 1;
double angle1 = Math.Atan(angleY / angleX);
int xoff = (int)Math.Cos(angle1) * safeRadius;
if (p1.X > p2.X)
xoff = -xoff;
double yoff = Math.Sin(angle1) * safeRadius;
if (p1.Y > p2.Y)
yoff = -yoff;
gfx.DrawLine(p, p1.X + xoff, p1.Y + yoff, p2.X - xoff, p2.Y - yoff);
I was debating if I should be posting this in the drawing newsgroup but
finally decided that it wasn't drawing related (even if that's what I'm
ultimatly doing) but instead optimization / math. In a sense it's not
c-sharp specific either but I hope someone can share some suggestions. So
what I'm doing can be viewed as graphis a graph (Vertex and Edges).
I have a series of points drawn on a bitmap and some of them should be
connected with a line. That itself is no problem, it would be a basic
DrawLine call. However what I need to do is to have the line not start/end
in the center of each point but instead at a certain distance from it, i.e
having a sort of safe radius around each point
So instead of this (sorry for the poor ascii)
*----*
I want this
* ---- *
Now please note that this would not be a problem is all points where lined
up on the horizontal and vertical axis but they are not, meaning that I will
be drawing diagonale lines. So what I came up with was some basic
trigonomety to calculate where the line coordinates are. My problem is
performance and the need to optimize. I have about 5000 points and they are
all connected in a web. I have a need to redraw this when ever scrolling
occures (yeah I only redraw the things that are visible in my viewing pane)
and performance is a bit slow
So is there any math-tricks / optimizations anyone could recommend to do the
same? Below is the code I use to calculate and draw the line. For the sake
of the argument, assume I cannot cache the calculation after the first time
it's been drawn and have a need to run it each time
int safeRadius = 7;
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
double angleX = Math.Abs(p2.X - p1.X);
if (angleX == 0)
angleX = 1;
double angleY = Math.Abs(p2.Y - p1.Y);
if (angleY == 0)
angleY = 1;
double angle1 = Math.Atan(angleY / angleX);
int xoff = (int)Math.Cos(angle1) * safeRadius;
if (p1.X > p2.X)
xoff = -xoff;
double yoff = Math.Sin(angle1) * safeRadius;
if (p1.Y > p2.Y)
yoff = -yoff;
gfx.DrawLine(p, p1.X + xoff, p1.Y + yoff, p2.X - xoff, p2.Y - yoff);