2013-09-02 3 views
4

Im DDA (Digital Diferential Analizer)를 사용하여 선을 만들고, 내가 어쩌면 DrawLine을 사용하고 있는지 알고 있다고 생각했습니다. Im은 파선이나 점선과 같은 다양한 유형의 라인을 만들려고 노력하고 있습니다. 아래에서 for를 만들기 위해 생각한 것은 점선을 만들기 위해 숫자를 점프하는 것입니다. 그러나 나는 아직도 할 수있는 방법을 찾지 못한다. 이것은 내가 지금까지 가지고있는 것입니다 :디지털 Diferential Analizer 점선

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); 
     }*/ 

    } 

아이디어가 있으십니까?

+0

선 색상을 배경색으로 변경 한 다음 다시 검정색 (또는 선택한 다른 색상)으로 '점프'한 다음 배경색으로 다시 이동하십시오. 이렇게하면 효과를 얻을 수 있습니다. 그러나 이것은 또한 당신이 사용하는 레이어에 달려 있으며, 무엇이든 트랜스 패 런트 (transparant)라면 ... – mike

+0

와우 하임은 그 XD ty ill에 대해 생각해보십시오. –

+0

당신을 환영합니다! – mike

답변

1
  1. glue line 함수가 필요합니다 (g.drawline (x0, y0, x1, y1);).
  2. 하는 색상에 대한주의를 해달라고
  3. 당신은 예를 들어 INT 패턴 [] = {10, -5,0에 대한
  4. (픽셀 라인과 공간의 크기)하여 패턴의 정의를 필요로 (나중에 그것으로 재생할 수 있습니다) } (10px 라인, 5px 스페이스, 0은 처음부터 반복을 의미합니다.) - 값은 공백 + 값의 라인입니다.
  5. '글로벌'상태 (패턴의 실제 인덱스와 실제 픽셀 길이를 그립니다)가 필요합니다. 전역 패턴 포인터 또는 클래스/구조체에 모두 캡슐화.

    //--------------------------------------------------------------------------- 
    // 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, 리터를 재설정하는 것을 잊지 마세요 : 기본적인 아이디어는이 같은 예를 들어, 선택된 패턴에 대한 라인을 segmentate하는 것입니다, 그래서

확인. 코드가 최적화되어 있지 않아 매우 느리지 만 단순하지만 충분히 이해할 수 있습니다.