469,934 Members | 2,757 Online

# how to draw a dashed line in c

106 100+
can anybody tell me how to draw a dashed line in c

i've drawn a dotted one using a simple for but i can't figure out how to make it dashed ... i need a for loop that is increasing constantly but skips afew pixels in the way and then continue drawing again

here's the one i used to draw the dotted line
y=y1 where x1,y1 are start points x2,y2 end points
for( x=x1;x<=x2;x+=3)

{ putpixel(x,y,color);
}
Oct 26 '06 #1
4 11556
dtimes6
73
Try something like
Expand|Select|Wrap|Line Numbers
1. for( x1 = sx, y1 = sy; (x1 < ex)&&(y1 < ey); x1+=(dx*2), y1+=(dy*2)) {
2.     MoveTo(x1,y1); //Move To the pos don't draw line
3.     x2=x1+dx;
4.     y2=y1+dy;
5.     LineTo(x2,y2); //Line from (x1,y1) to (x2,y2)
6. }
7.
Oct 27 '06 #2
can anybody tell me how to draw a dashed line in c

i've drawn a dotted one using a simple for but i can't figure out how to make it dashed ... i need a for loop that is increasing constantly but skips afew pixels in the way and then continue drawing again

here's the one i used to draw the dotted line
y=y1 where x1,y1 are start points x2,y2 end points
for( x=x1;x<=x2;x+=3)

{ putpixel(x,y,color);
}
This is a very intresting ideea of drawing a line. I'll try it.
Sep 17 '07 #3
Try something like
Expand|Select|Wrap|Line Numbers
1. for( x1 = sx, y1 = sy; (x1 < ex)&&(y1 < ey); x1+=(dx*2), y1+=(dy*2)) {
2.     MoveTo(x1,y1); //Move To the pos don't draw line
3.     x2=x1+dx;
4.     y2=y1+dy;
5.     LineTo(x2,y2); //Line from (x1,y1) to (x2,y2)
6. }
7.

i m trying to solve this problem using c code
for(k=1;k<steps;k++)
{
x+=xinc;
y+=yinc;
if(cnt==0 || cnt > 5)
{
putpixel(x,y,15);
}
if(cnt >= 5 )
{
for(i=0;i<5;i++)
{
putpixel(x,y,0);
}
cnt=0;
}
}
problem is that only one space of blank space will be considered in dashed line.
Jul 10 '08 #4
hdanw
61
A dash is made up of dots.

Try putting your dots really close to gether for a while, then skip some space, and put them close together for another while...

Should look somehting like this:

..... ..... ..... ..... ..... .....
Jul 12 '08 #5