我用数字微分分析器(Digital Diferential Analizer)画一条线,我想我知道可能会像现在这样使用de DrawLine,就跟着它跑吧。我试着画不同类型的线,比如虚线或虚线等。我想在画for时从下面跳一些数字来画一条虚线。但我仍然找不到方法。这就是我目前所拥有的:
public void paint(Graphics g) {
super.paint(g);
int dot=0;
int x1 = pointStart.x;
int x2 = pointEnd.x;
int y1 = pointStart.y;
int y2 = pointEnd.y;
float dx, dy, m, y, x;
if (x1>x2){
int ax = x2;
int ay = y2;
x2 = x1;
x1 = ax;
y2 = y1;
y1 = ay;
}
dx = x2 - x1;
dy = y2 - y1;
m = dy/dx;
if (m>=-1&&m<=1){
dot = (int)dx/4;
y = y1;
System.out.println(m);
for (x = x1 ; x <= x2;x++){
//if (x>=dot&&x<=dot+10||x>=dot*2&&x<=dot*2+10||x>=dot*3&&x<=dot*3+10){
g.drawLine((int)x, (int)Math.round(y), (int)x, (int)Math.round(y));
y+=m;
//}
}
}
else{
x = x1;
System.out.println(m);
for (y = y1 ; y <= y2;y++){
g.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
x+=1/m;
}
}
/*if (pointStart != null) {
if (x1>)
g.setColor(Color.RED);
//g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
g.drawLine(x1, y1, x1, y1);
}*/
}
有什么想法吗?
- 您需要绘制线函数(在您的情况下为g.drawline(x0,y0,x1,y1);)
- 不在乎颜色(你以后可以玩)
- 你需要定义你的图案(以像素为单位的线条和空格的大小)
- 例如int pattern[]={10,-5,0}(10px行,然后5px空格,0表示从头开始重复)-值为空格+值为线条
- 您需要"全局"状态(模式中的实际索引和绘制的实际像素长度),还可以有全局模式指针或将所有指针封装在类/结构中
好的,所以基本的想法是将任何一条线分割成选定的图案,例如:
//---------------------------------------------------------------------------
// pattern draw state
int _pattern_ix=0; // actual index in pattern need to reset it to zero before any pattern change
double _pattern_l=0; // already drawed or skipped pixels from actual pattern[_pattern_ix]
// predefined patterns
int _pattern_dash_dash[]={ 10,-10, 0 };
int _pattern_dash_dot[] ={ 10,- 5, 1,- 5,0 };
int _pattern_dot_dot[] ={ 1,- 5, 0 };
//---------------------------------------------------------------------------
// draw line function
void drawline(int x0,int y0,int x1,int y1)
{
// this is just borland GDI access to draw line function
Form1->Canvas->MoveTo(x0,y0);
Form1->Canvas->LineTo(x1,y1);
}
//---------------------------------------------------------------------------
void pattern_line(int x0,int y0,int x1,int y1,int *pattern)
{
int p;
double x,y,xx,yy,dx,dy,dl,t,dt;
dx=x1-x0;
dy=y1-y0;
dl=sqrt((dx*dx)+(dy*dy));
dx/=dl; dy/=dl;
for (t=0.0,dt=0.0;dl>=0.5;)
{
p=pattern[_pattern_ix];
if (p<0) // skip
{
dt=-p-_pattern_l; // t=space to skip [px]
if (dt>dl) { _pattern_l+=dl; return; } // space is bigger then rest of line
dl-=dt; t+=dt; _pattern_l=0.0; // update line params and continue to next pattern entry
}
else // draw
{
dt=+p-_pattern_l; // t=space to draw [px]
x=x0+double(t*dx); // actual point pos
y=y0+double(t*dy); // space is bigger then rest of line
if (dt>dl) { _pattern_l+=dl; drawline(x,y,x1,y1); return; }
dl-=dt; t+=dt; _pattern_l=0.0; // update line params
xx=x0+double(t*dx); // actual point pos
yy=y0+double(t*dy);
drawline(x,y,xx,yy); // draw line and continue to next pattern entry
}
_pattern_ix++;
if (!pattern[_pattern_ix]) _pattern_ix=0;
}
}
//---------------------------------------------------------------------------
void main()
{
// borland GDI clear screen and color settings
Canvas->Brush->Color=clBlack;
Canvas->Pen->Color=clWhite;
Canvas->FillRect(ClientRect);
// draw dash-dot-ed rectangle
int x0,x1,y0,y1;
x0=30; x1=200;
y0=30; y1=100;
pattern_line(x0,y0,x1,y0,_pattern_dash_dot);
pattern_line(x1,y0,x1,y1,_pattern_dash_dot);
pattern_line(x1,y1,x0,y1,_pattern_dash_dot);
pattern_line(x0,y1,x0,y0,_pattern_dash_dot);
}
//---------------------------------------------------------------------------
并且不要忘记在任何模式样式更改之前将模式ix,l重置为零。代码没有经过优化,所以它很慢,但我希望它足够简单,可以理解。