我想在向量中包含的点之间绘制线段并将它们显示在窗口中。
目前,这就是我所拥有的:
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
,然后使用LineTo
andMoveToEx
转到下一个点并从前一个点画一条线。
目前我得到一个完全空白的窗口,没有错误。建议?
编辑:
所以我猜下面评论中提到的断点消息是由我加载外部向量引起的。for
该向量是另一个 .CPP 文件中循环的输出。
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:
所以我从折线 API 开始。const POINT*
我创建了一个称为类型的数组,并Pt
尝试将值分配给它。然后我打电话告诉它从数组中提取。t
y_n
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);
从另一个 CPP 文件:
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"
}
编辑 3:
我现在正试图用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"
}
错误是针对这一行的:
Pt.insert(Pt.begin(),t);
编辑:请参阅此帖子以获取答案。