Reputation: 467
I want to draw line segments between points contained in a vector and display them in a window.
Currently, this is what I have:
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);
This is contained in a larger .CPP source file generated as when creating a standard Win32 Application.
As you can see, my idea was to iterate through my vector using a for
loop and then use LineTo
and MoveToEx
to go to the next point and draw a line form the previous one.
Currently I get a completely blank window with no errors. Suggestions?
EDIT:
So I'm guessing the break point message mentioned in the comments below was caused by my loading the external vector. The vector is an output of a for
loop in another .CPP file.
using std::vector;
using std::iterator;
extern vector<int> euler_time;
extern vector<int> euler_soln;
And the loop in the other .CPP file:
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);
}
EDIT 2:
So I started with the Polyline API. I created an array of type const POINT*
called Pt
and attempted to assign the values t
and y_n
to it. I then called Polyline
and told it to draw from the array.
I end up with no errors and a blank window again.
From the Window CPP file:
extern const POINT* Pt;
//Draw lines to screen.
hPen = CreatePen(PS_SOLID, 1, RGB(255, 25, 5));
SelectObject(hdc, hPen);
Polyline(hdc,Pt,10);
From the other CPP file:
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"
}
EDIT 3:
I'm now trying to use vector<POINT>
to create a vector with the point values. However, my attempt results in the following error:
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
My code is below:
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"
}
The error is for this line:
Pt.insert(Pt.begin(),t);
EDIT: See this post for an answer.
Upvotes: 0
Views: 2205
Reputation: 28839
This is because you are drawing line segments of length zero.
Only do a MoveTo on the first point, then do LineTo on the rest.
Upvotes: 2