在矢量中包含的点之间绘制线条(VC ++)

时间:2015-02-05 19:37:17

标签: c++ visual-studio-2012 hdc pen

我想在矢量中包含的点之间绘制线段,并在窗口中显示它们。

目前,这就是我所拥有的:

case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
  //Draw lines to screen.
    using std::vector;
    using std::iterator;
    extern vector<int> euler_time;
    extern vector<int> euler_soln;
    hPen = CreatePen(PS_SOLID, 1, RGB(255, 25, 5));
    SelectObject(hdc, hPen);
    for(std::vector<int>::size_type i = 0; i != euler_time.size(); i++){
    MoveToEx(hdc,euler_time[i],euler_soln[i],NULL);
    LineTo(hdc,euler_time[i],euler_soln[i]);
    }
    EndPaint(hWnd, &ps);

这包含在创建标准Win32应用程序时生成的更大的.CPP源文件中。

正如您所看到的,我的想法是使用for循环遍历我的向量,然后使用LineToMoveToEx转到下一个点并从中绘制一条线前一个。

目前我得到一个完全空白的窗口,没有错误。建议?

编辑:

所以我猜测下面评论中提到的断点消息是由我加载外部向量引起的。向量是另一个.CPP文件中for循环的输出。

    using std::vector;
    using std::iterator;
    extern vector<int> euler_time;
    extern vector<int> euler_soln;

另一个.CPP文件中的循环:

for (double t = a; t < b; t += h )
{
    std::cout << std::fixed << std::setprecision(3) << t << " " << y << "\n";
    euler_time.insert (euler_time.begin(),t); // Insert the values of t and y into the respective vectors.
    euler_soln.insert (euler_soln.begin(),y);
    y += h * f(t, y);
}

编辑2:

所以我开始使用Polyline API。我创建了一个名为const POINT*的{​​{1}}类型的数组,并尝试为其分配值Ptt。然后我调用y_n并告诉它从数组中绘制。

我最终没有错误,再次出现空白窗口。

来自Window CPP文件:

Polyline

来自其他CPP文件:

    extern const POINT* Pt;
  //Draw lines to screen.
    hPen = CreatePen(PS_SOLID, 1, RGB(255, 25, 5));
    SelectObject(hdc, hPen);
    Polyline(hdc,Pt,10);

编辑3:

我现在正尝试使用const POINT * Pt; void euler(F f, int y0, int a, int b, int h ) // defines a class of type "void" (returns nothing); gives it parameters: function F, doubles: y0, a, b, h { int y_n = y0; int t = a; for (int t = a; t < b; t += h ) // creates a for loop beginning with time t = a and ending with t ~= b with stepsize h. { std::cout << std::fixed << std::setprecision(3) << t << " " << y_n << "\n"; // Calls the standard output from std, with floating-point numbers with precision 3; assigns the variable t, then a space, then variable y, then a new line. LONG x = t; LONG y = y_n; y_n += h * f(t, y_n); // y increases by h * f(t,y) where f is the derivative y' until the condition is met y ~= b. } std::cout << "done\n"; // Print "done" } 创建带有点值的向量。但是,我的尝试导致以下错误:

vector<POINT>

我的代码如下:

6   IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=POINT, _Alloc=std::allocator<POINT>]" matches the argument list
        argument types are: (std::_Vector_iterator<std::_Vector_val<std::_Simple_types<POINT>>>, double)
        object type is: std::vector<POINT, std::allocator<POINT>>   c:\Users\ahlroth\Documents\Visual Studio 2012\Projects\Euler\Euler\eulerclass.cpp   22

}

该行的错误是: using std::vector; vector<POINT> Pt; POINT euler(F f, double y0, double a, double b, double h, vector<POINT> Pt) // defines a class of type "void" (returns nothing); gives it parameters: function F, doubles: y0, a, b, h { double y_n = y0; double t = a; for (double t = a; t < b; t += h ) // creates a for loop beginning with time t = a and ending with t ~= b with stepsize h. { std::cout << std::fixed << std::setprecision(3) << t << " " << y_n << "\n"; // Calls the standard output from std, with floating-point numbers with precision 3; assigns the variable t, then a space, then variable y, then a new line. Pt.insert(Pt.begin(),t); y_n += h * f(t, y_n); // y increases by h * f(t,y) where f is the derivative y' until the condition is met y ~= b. } return Pt; std::cout << "done\n"; // Print "done"

编辑:请参阅此帖子了解answer.

1 个答案:

答案 0 :(得分:2)

这是因为您正在绘制长度为零的线段。

仅在第一个点上执行MoveTo,然后在其余点上执行LineTo。