流氓线被拉到窗口 [英] Rogue line being drawn to window

查看:148
本文介绍了流氓线被拉到窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SFML库在C ++中制作图形程序。到目前为止,我已经能够在屏幕上绘制一个功能。一路上遇到两个问题。
第一条线似乎是从我的函数结束开始返回到我的飞机的原点。



您可以在此图片中看到它:



正如你所看到的,这个流氓行似乎在接近原点时会改变颜色。我的第一个问题是这条线是什么,我怎么能从我的窗户中消除它?



第二个问题可以在这张图片中看到: =https://i.stack.imgur.com/tl0Ua.png =nofollow noreferrer>



正如您所看到的渐近线是绘制的图形未定义或不连续的点。这导致我的第二个问题:有没有一种方法(代码中)来识别渐近线,而不是将其绘制到窗口中。



窗口是:

  VertexArray轴(Lines,4); 
VertexArray曲线(PrimitiveType :: LinesStrip,1000);

axis [0] .position = Vector2f(100000,0);
axis [1] .position = Vector2f(-100000,0);
axis [2] .position = Vector2f(0,-100000);
轴[3] .position = Vector2f(0,100000);

float x; (x = -pi; x {
curve.append(Vertex(Vector2f(x, ),Color :: Green));
}

我非常感谢任何输入:)

更新:

感谢许多人的输入,此代码似乎可以很好地解决渐近线问题:

pre $ for(x = -30 * pi; x <30 * pi; x + = .0005f)
{
x0 = x1; y0 = y1;
x1 = x; y1 = -1 / sin(x);
a = 0;
a = fabs(atan2(y1 - y0,x1 - x0));
if(a> .499f * pi)
{
curve.append(Vertex(Vector2f(x1,y1),Color :: Transparent));
}
else
{
curve.append(Vertex(Vector2f(x1,y1),Color :: Green));
}
}

更新2:



下面的代码摆脱了流氓行:

  VertexArray曲线(线,1000); 
float x,y; (x = -30 * pi; x <30 * pi; x + = .0005f)

{
y = -asin(x);
curve.append(Vertex(Vector2f(x,y))); (x = -30 * pi + .0005f; x <30 * pi; x + = .0005f)
}


y = -asin(x)
curve.append(Vertex(Vector2f(x,y)));


解决方案

第一个问题看起来像一个错误的折线/曲线处理。不知道你使用了什么 API 进行渲染,但有些像 GDI 需要正确启动位置。例如,如果你这样画:

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

然后您应该这样做:

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

如果您的 API 需要 MoveTo 命令并且你没有设置它,那么使用上一个位置(或默认(0,0)),这将连接开始您的曲线与上次绘制或默认笔位置的直线一致。

第二个问题

在连续数据中,您可以通过检查后续 y 值来确定渐近线或不连续点。如果你的曲线渲染看起来像这样:

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

然后您可以将其更改为如下所示:

  y0 = y [0] + 2 *阈值;如果(y [1] -y0> =阈值)Canvas-> MoveTo(x [i],y [一世]); 
else Canvas-> LineTo(x [i],y [i]);
y0 = y [i];

$ / code>

问题是选择阈值,因为它依赖于 x 采样点密度以及 y 数据的第一次推导 x (斜角)

如果叠加更多函数,曲线附加会创建不需要的行......而是将每个数据作为单独的绘图处理,或将 MoveTo 命令在它们之间



[Edit1]



我看到这样(假分裂):

  double x0,y0,x1,y1,a; (e = 1,x = -pi; x  {
//最后一点
x0 = x1; Y0 = Y1;
//您的实际点
x1 = x; Y1 = -tan(X);
//测试不连续性
if(e){a = 0; E = 0; } else a = fabs(atan2(y1-y0,x1-x0)); (a> 0.499 * M_PI)curve.append(Vertex(Vector2f(x1,y1),Color :: Black));

else curve.append(Vertex(Vector2f(x1,y1),Color :: Green));

0.499 * M_PI 是你的门槛越接近 0.5 * M_PI 它检测到的更大的跳跃...我伪造了黑色(背景)分割的曲线,它会在轴上创建空隙交点(除非使用透明度)......但不需要曲线列表...

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 a line which seems to return to the origin of my the plane, starting from the end of my function.

You can see it in this image:

As you can see this "rogue" line seems to change colour as it nears the origin. My first question is what is this line and how may I eradicate it from my window?

The second problem which is slightly unrelated and more mathematical can be seen in this image:

As you can see the asymptotes which are points where the graph is undefined or non continuous are being drawn. This leads me to my second question: is there a way ( in code ) to identify an asymptote and not draw it to the window.

My code for anything drawn to the window is:

VertexArray axis(Lines, 4);
VertexArray curve(PrimitiveType::LinesStrip, 1000);

axis[0].position = Vector2f(100000, 0);
axis[1].position = Vector2f(-100000, 0);
axis[2].position = Vector2f(0, -100000);
axis[3].position = Vector2f(0, 100000);

float x;

for (x = -pi; x < pi; x += .0005f)
{
    curve.append(Vertex(Vector2f(x, -tan(x)), Color::Green));
}

I would very much appreciate any input : )

Update:

Thanks to the input of numerous people this code seems to work fine in fixing the asymptote problem:

for (x = -30*pi; x < 30*pi; x += .0005f)
{
    x0 = x1; y0 = y1;
    x1 = x; y1 = -1/sin(x);
    a = 0; 
    a = fabs(atan2(y1 - y0, x1 - x0));
    if (a > .499f*pi)
    {
        curve.append(Vertex(Vector2f(x1, y1), Color::Transparent));
    }
    else
    {
        curve.append(Vertex(Vector2f(x1, y1), Color::Green));
    }
}

Update 2:

The following code gets rid of the rogue line:

VertexArray curve(Lines, 1000);
float x,y;
for (x = -30 * pi; x < 30 * pi; x += .0005f)
{
    y = -asin(x);
    curve.append(Vertex(Vector2f(x, y)));
}
for (x = -30 * pi + .0005f; x < 30 * pi; x += .0005f)
{
    y = -asin(x);
    curve.append(Vertex(Vector2f(x, y)));
}

解决方案

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 ...

这篇关于流氓线被拉到窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆