Rogue line being drawn to window

后端 未结 2 1041
走了就别回头了
走了就别回头了 2021-01-21 01:47

I am making a graphing program in C++ using the SFML library. So far I have been able to draw a function to the screen. I have run into two problems along the way. The first is

相关标签:
2条回答
  • 2021-01-21 02:20

    The first problem looks like a wrong polyline/curve handling. Don't know what API are you using for rendering but some like GDI need to start the pen position properly. For example if you draw like this:

    Canvas->LineTo(x[0],y[0]);
    Canvas->LineTo(x[1],y[1]);
    Canvas->LineTo(x[2],y[2]);
    Canvas->LineTo(x[3],y[3]);
    ...
    

    Then you should do this instead:

    Canvas->MoveTo(x[0],y[0]);
    Canvas->LineTo(x[1],y[1]);
    Canvas->LineTo(x[2],y[2]);
    Canvas->LineTo(x[3],y[3]);
    ...
    

    In case your API needs MoveTo command and you are not setting it then last position is used (or default (0,0)) which will connect start of your curve with straight line from last draw or default pen position.

    Second problem

    In continuous data you can threshold the asymptotes or discontinuities by checking the consequent y values. If your curve render looks like this:

    Canvas->MoveTo(x[0],y[0]);
    for (i=1;i<n;i++) Canvas->LineTo(x[i],y[i]);
    

    Then you can change it to something like this:

    y0=y[0]+2*threshold;
    for (i=0;i<n;i++)
     {
     if (y[1]-y0>=threshold) Canvas->MoveTo(x[i],y[i]);
      else                   Canvas->LineTo(x[i],y[i]);
     y0=y[i];
     }
    

    The problem is selection of the threshold because it is dependent on x density of sampled points and on the first derivation of your y data by x (slope angles)

    If you are stacking up more functions the curve append will create your unwanted line ... instead handle each data as separate draw or put MoveTo command in between them

    [Edit1]

    I see it like this (fake split):

    double x0,y0,x1,y1,a;
    for (e=1,x = -pi; x < pi; x += .0005f)
        {
        // last point
        x0=x1; y0=y1;
        // your actual point
        x1=x; y1=-tan(x);
        // test discontinuity
        if (e) { a=0; e=0; } else a=fabs(atan2(y1-y0,x1-x0));
        if (a>0.499*M_PI) curve.append(Vertex(Vector2f(x1,y1), Color::Black));
         else             curve.append(Vertex(Vector2f(x1,y1), Color::Green));
        }
    

    the 0.499*M_PI is you threshold the more closer is to 0.5*M_PIthe bigger jumps it detects... I faked the curve split by black color (background) it will create gaps on axis intersections (unless transparency is used) ... but no need for list of curves ...

    0 讨论(0)
  • 2021-01-21 02:35

    Those artifacts are due to the way sf::PrimitiveType::LinesStrip works (or more specific lines strips in general).

    In your second example, visualizing y = -tan(x), you're jumping from positive infinity to negative infinity, which is the line you're seeing. You can't get rid of this, unless you're using a different primitive type or splitting your rendering into multiple draw calls.

    Imagine a line strip as one long thread you're pinning with pushpins (representing your vertices). There's no (safe) way to go from positive infinity to negative infinity without those artifacts. Of course you could move outside the visible area, but then again that's really specific to this one function.

    0 讨论(0)
提交回复
热议问题