数字差示Analizer点状线 [英] Digital Diferential Analizer Dotted Line
问题描述
即时通讯使用德DDA(数字差示Analizer)做出一条线了,以为我知道,也许使用去的DrawLine我的方式,只需要运行与它一起。我试着去做出不同的类型,如虚线或点,等林线makeing的从下面跳一些数字,使虚线思考。但是,我还是不能找到一种方法,待办事项它。这是我到目前为止有:
公共无效漆(图形G){
super.paint(G);
INT点= 0;
INT X1 = pointStart.x;
INT X2 = pointEnd.x;
INT Y1 = pointStart.y;
INT Y2 = pointEnd.y;
浮DX,DY,M,Y,X;
如果(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;
如果(M> = - 1安培;&安培; M< = 1){
点=(int)的DX / 4;
Y = Y1;
的System.out.println(米);
为(X = X1; X - 其中= X2; X ++){
//如果(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 + =米;
//}
}
}
其他{
X = X1;
的System.out.println(米);
对于(Y = Y1; Y&LT; = Y 2,Y ++){
g.drawLine((int)的Math.round(x)时,(INT)Y,(int)的Math.round(x)时,(INT)y)基
X + = 1 /米;
}
}
/ *如果(pointStart!= NULL){
如果(X1&GT;)
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模式[] = {10,-5,0}(10px的线,然后5px的空间,0是指从开始重复)... - 值是空格+值线
- 您需要的全球性的状态(在模式和实际像素长度drawed实际索引),你也可以拥有全球格局指针或封装所有的类/结构。
确定这样的基本思路是segmentate任何行选择的模式,例如是这样的:
// ------------------------------------ ---------------------------------------
//图案绘制状态
INT _pattern_ix = 0;在模式//实际的指数需要在任何模式的改变将其重置为零
双_pattern_l = 0; //已经drawed还是从实际的模式[_pattern_ix]跳过像素
// predefined模式
INT _pattern_dash_dash [] = {10,-10,0};
INT _pattern_dash_dot [] = {10, - 5,1, - 5,0};
INT _pattern_dot_dot [] = {1, - 5,0};
// ------------------------------------------------ ---------------------------
//画线功能
无效的DrawLine(INT X0,Y0 INT,INT X1,Y1 INT)
{
//这只是borland的GDI进入画线功能
Form1-&GT; Canvas-&GT;通过MoveTo(X0,Y0);
Form1-&GT; Canvas-&GT; LineTo函数(X1,Y1);
}
// ------------------------------------------------ ---------------------------
无效pattern_line(INT X0,Y0 INT,INT X1,Y1 INT,INT *模式)
{
INT磷;
双X,Y,XX,YY,DX,DY,DL,T,DT;
DX = X1-X0;
DY = Y1-Y0;
DL =开方((DX * DX)+(DY * DY));
DX / =升; DY / =升;
对于(T = 0.0,DT = 0.0; DL&GT; = 0.5;)
{
P =模式[_pattern_ix]
如果(P&小于0)//跳过
{
DT = -p,_pattern_l; // T =空间来跳过[PX]
如果(DT&GT; DL){_pattern_l + =升;返回; } //空间更大然后行休息
DL- = DT; T + = DT; _pattern_l = 0.0; //更新行PARAMS,继续下一个模式进入
}
否则//画
{
DT = + P-_pattern_l; // T =空间绘制[PX]
X = X0 +双(T * DX); //实际点的POS
Y = Y0 +双(T * DY); //空间更大然后行休息
如果(DT&GT; DL){_pattern_l + =升; DrawLine的(X,Y,X1,Y1);返回; }
DL- = DT; T + = DT; _pattern_l = 0.0; //更新行PARAMS
XX = X +双(T * DX); //实际点的POS
YY = Y0 +双(T * DY);
DrawLine的(X,Y,XX,YY); //画线,并继续到下一个模式进入
}
_pattern_ix ++;
(!图案[_pattern_ix])如果_pattern_ix = 0;
}
}
// ------------------------------------------------ ---------------------------
无效的主要()
{
// Borland的GDI清晰的屏幕和颜色设置
Canvas-&GT;电刷&GT;颜色= clBlack;
Canvas-&GT;养老金&GT;颜色= clWhite;
Canvas-&GT; fillRect方法(ClientRect);
//画点划线-ED矩形
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);
}
// ------------------------------------------------ ---------------------------
和不要忘了之前的任何图案样式的变化来重置模式九,l至零。 code未优化,以便其pretty的慢,但非常简单明白我的希望。
Im using de DDA (Digital Diferential Analizer) to make a line, and thought I know maybe using de DrawLine the way I am, just run along with it. Im trying to make different types of lines like dashed or dotted, etc. Im thinking in makeing the for from below jump some numbers to make a dotted line. But I cant still find a way todo it. This is what I have so far:
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);
}*/
}
Any ideas?
- you need draw line function (in your case g.drawline(x0,y0,x1,y1); ).
- dont care about color (you can play with it later)
- you need definition of your pattern (size of lines and spaces in pixels)
- for example int pattern[]={10,-5,0} (10px line, then 5px space, 0 means repeat from beginning) ... -values are spaces + values are lines
- you need 'global' state (actual index in pattern and actual pixel length drawed) also you can have global pattern pointer or encapsulate all in class/struct.
ok so the basic idea is to segmentate any line to selected pattern for example like this:
//---------------------------------------------------------------------------
// 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);
}
//---------------------------------------------------------------------------
and do not forget to reset pattern ix,l to zero before any pattern style change. Code is not optimized so its pretty slow but simple enough to understand i hope.
这篇关于数字差示Analizer点状线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!