Older Version Newer Version

tsh73 tsh73 Jul 31, 2010

=Graphics 101 – plotting a function= - by tsh73 - {$creationdate} [[toc]] ---- ==Goal== (and history). Once upon a time there was a person who wanted to plot some graphs. He supposedly knew his math well, but just never draw a thing in Basic. Of course he was directed to a help file; but he said "I have read through the help files on graphics and it just confuses me". Well, no wonder – help is a reference in a first place, so it's more or less OK to dump all graphic functions into one BIG page *shrugs*. There is tutorial in LB, but it surprisingly focuses on turtle graphics, and leaves plotting for the reader so to say. So I thought I could as well write tutorial myself, with intent to cover "starting from open for graphics, via pixels, coordinates, points and lines. I guess that's all really needed, may be add printing text for labeling and flush." So here I am. Forgive my Clatchian English. ==Non-goals== I am not going to discuss GUI or animation or sprites – I just going to teach how to draw a static picture. Namely, to plot a function. ==Pre-requisites== I suppose you already are "master of mainwin". You can calculate, check conditions, loop and print results. These are really basics, so if not, go to part one of LB / JB tutorial. ==Where to draw== So, suppose prior to this you worked in mainwin. It is nice but it is not designed to draw stuff on – only to print text. So, we need something to draw upon. There two types of things that allow drawing on itself – it's a window opened for graphics, and graphicbox added to ordinary window. You can have several graphicboxes on a window and draw in all of them. Pretty cool, but for now we take simplest course – [[code format="vbnet"]] open "test" for graphics as #gr #gr "trapclose [quit]" wait [quit] close #gr end [[code]] That's about as simple as it gets. First line opens window titled "test", on which we can draw. On my computer, it's about 300x300 pixels (more about pixels later) (you can try to type for graphics_nsb_nf instead and get even simpler window) Second install trapclose handler – names a label which will get called on closing window. Third prevent our program from closing immediately :) And two last lines are called then window is closed by Alt-F4 or by mouse – they explicitly close window and end program. (Without that, you can end up with no visible windows BUT Basic still running in memory). Two more things. If you do not need mainwin anymore, you type nomainwin on top of your program. And if you need more space, you set WindowWidth and WindowHeight before opening window. ==Pixels and coordinates== Ahhh. Pixels. That is it, exactly? They are just dots on your monitor. Anything you see on a monitor composed from colored dots, named pixels. The pixel is smallest element of a picture. Your monitor just now has current resolution, like 1280x1024. It's size of screen in pixels. ("just now" 'cause it could be changed). And it's maximal size of a window you can show (and draw upon). Each pixel has coordinates. Basically that means they are numbered from 0, left to right and top to bottom. With whole numbers – there are no pixels between 0 and 1. *pic1* (On math they teach another coordinate plane, there axes go from left to right and from bottom to top. And where are plenty place between whole points. *pic2* We are going to respect that then plotting functions). ==Make a dot== ==Third Part Title== Text here. [[code format="vbnet"]] 'code here [[code]]