Working through the zetcode wxwidgets tutorial:Device Contexts:Point

This pc of sample code is sort of fun… It brings back lots of memories.. from my qbasic days…. (Oh boy… am I dating myself…)
Anyway… Need to load it up and see what catches my eye.

#include “points.h”
#include
#include

Points::Points(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

this->Connect(wxEVT_PAINT, wxPaintEventHandler(Points::OnPaint));
srand(time(NULL));
this->Centre();
}

void Points::OnPaint(wxPaintEvent & event)
{
wxPaintDC dc(this);

wxCoord x = 0;
wxCoord y = 0;

wxSize size = this->GetSize();

for (int i = 0; i<1000; i++) {
x = rand() % size.x + 1;
y = rand() % size.y + 1;
dc.DrawPoint(x, y);
}
}

These programs bring back some memories… I remember doing some art work with some quick basic on my original IBM …. Poor old thing when to  recyclers this weekend ;(
I guess the thing that catches my eye is not so much wx stuff… but this
srand(time(NULL));

Pretty standard stuff… Just the C++ version of doing things.

The more I see this stuff, the more I’m starting to like it.
Trying to deal with: “wxSize size = this->GetSize(); ” kind of stuff in Vb was always pain.

x = rand() % size.x + 1;

For some reason, I always had disdain over the modulus operator.. It just seems wrong.. But I know it’s used alot in computer science so I guess I need to deal with it.
Mentally I’m having a difficulty wrapping my head around why the modulus “%” operator works here
Sometimes things are just a little too succinct.. Maybe I should take the time to understand whats going on here..

I was doing some surfing at lunch a I came across this very interesting post by a guy called phridge back in 01.
Ok.. so…

I think you misunderstand what the modulo does. Modulo evaluates the following: a % b = c => b*k + c = a. Where we find some integer k that satisfies that relation. In the case of non-negative integers this is the same as the remainder of dividing a and b. The remainder is 2 in your case, since 18 % 8 = 2 => 8*2 + 2 = 18. Not .25, which is 2 / 8. Or, IOW, the remainder divided by the dividend.

That makes sense
x = rand() % size.x + 1;
Focusing in on:
rand() % size.x

a % b = c => b*k + c = a.

I guess I should study a little about the wxString functions to help adding debugging text
http://docs.wxwidgets.org/stable/wx_wxstringoverview.html

Ok… Here is a little tidbit that sounds interestings:
The usage of std::string compatible functions is strongly advised! It will both make your code more familiar to other C++ programmers (who are supposed to have knowledge of std::string but not of wxString), let you reuse the same code in both wxWidgets and other programs (by just typedefing wxString as std::string when used outside wxWidgets) and by staying compatible with future versions of wxWidgets which will probably start using std::string sooner or later too.

That actually is rather pragmatic and makes sense.
Ok.. This sort of an interesting wiki link

At this point, I just want to use cout with wxstrings (not claiming  I know what I’m doing here… But this looks interesting: http://lists.wxwidgets.org/pipermail/wx-users/2007-April/099124.html

This link discusses how to use cout with wxString..
Ok.. I guess I should give it a go..
Back to the rand().  I think I just figured out what is causing my confusion…..
In vb6 the rnd returns a single that between 0 and 1. That’s not what’s going on with rand since its return a int between 0 to at least 32767 depending on implemention.

Ok… At the risk of sounding like a bushim…. I unconfused myself :)… Finally, I can advance to the next module..

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *