Qbasic, Quickbasic/qbasic waves

Advertisement


Question
picture
picture  
how i make a graph for example ricture 1; i use line or pset; the x and y come from an difficult fuction so i have only the numbers of them,which i found in a programme in q basic. thanks!

Answer
Hi Ariel,

Drawing linegraphs are pretty easy in QBasic if you are familiar with how QBasic handles graphics. You will want to be familiar with the SCREEN statement and the LINE statement.

When you are dealing with graphics in QBasic, you will always need to set the SCREEN to a graphics mode.  QBasic supports DOS screen modes 0-13, which are pretty low resolution compared to modern graphics ability.  Mode 0 is a text-only mode, which QBasic uses by default, but it will not let you use graphics commands, like LINE, which we want, so we'll have to pick a mode between 1 and 13.  Here are a list of their attributes:

SCREEN 1 - 320x200, 16 colours (assigned to 4 attributes)
SCREEN 2 - 640x200, 16 colours (assigned to 2 attributes)
SCREEN 3 - 720 x 348, palette mode not supported, hercules graphics adapter only, may not be emulated properly
SCREEN 4 - 640x400, 16 colours (assign one to foreground, background is fixed to black)
SCREEN 5 - Not supported
SCREEN 6 - Not supported
SCREEN 7 - 320x200, 16 colours (assigned to 16 attributes)
SCREEN 8 - 640x200, 16 colours (assigned to 16 attributes)
SCREEN 9 - 640x350, 16 colours (assigned to 4 attributes with 64K adapter memory), or 64 colours (assigned to 16 attributes with more than 64K adapter memory)
SCREEN 10 - 640x350, 9 pseudo-colours (assigned to 4 attributes), monochrome
SCREEN 11 - 640x480, 256000 colours (assigned to 2 attributes)
SCREEN 12 - 640x480, 256000 colours (assigned to 16 attributes)
SCREEN 13 - 320x200, 256000 colours (assigned to 256 attributes)

Typically for graphing, I would probably pick SCREEN 12 since it has the highest standard resolution, and the most colour attributes per colour within that resolution.

Next, you'll need to understand the coordinate system in QBasic.  QBasic always assigns (0,0) to the top-left of the screen, and the maximum resolution to the bottom right.  For instance, the bottom-right would be (640,480) in SCREEN 12 .

Now that you understand the basics, we'll have to understand some methods of graphing.  For instance, the first problem you will run into is how do you scale the coordinates.  You will almost never want a 1:1 scale.  For instance, the graph you have attached has a range of 0.4 to 1.0 on the y axis, and 0.435 to 0.7682 on the x axis.  QBasic only understands graphics in terms of integers (no decimals), so we'll have to scale the units of our graph.  This is pretty easy math, which would look like:

DIM x_position AS SINGLE
DIM y_position AS SINGLE
x_position = ( ( (x_value - lowest_x_value) / (highest_x_value - lowest_x_value) ) * CSNG( pixel_width_of_graph ) ) + CSNG( pixel_x_offset_of_graph )
y_position = ( ( (y_value - lowest_y_value) / (highest_y_value - lowest_y_value) ) * CSNG( pixel_height_of_graph ) ) + CSNG( pixel_y_offset_of_graph )


So there are a few things to break down in this code.  First off, we have our output coordinates (x_position , y_position ).  Notice that I used SINGLE instead of INTEGER; this is because we want to keep as much precision in our coordinates as possible until we actually draw the graph.

Second, we have x_value and y_value - these should also be SINGLE values, which are the literal values we are graphing (ie, between 0.4 and 1.0 for y, and between 0.435 and 0.7682 for x).

The values lowest_x_value , highest_x_value , lowest_y_value , and highest_y_value all represent the ranges in the graph (ie, between 0.4 and 1.0 for y, and between 0.435 and 0.7682 for x).

The last variables control the section of screen we will use to draw the graph (which are INTEGERs, that we have wrapped in the CSNG function, which converts them to SINGLEs).  We have the pixel_width_of_graph , and pixel_height_of_graph , which are the literal height and width of your graph in pixels.  In SCREEN 12 , you'll want at most these values to be 640 for the height, and 480 for the width.  You may want to actually use 20 pixels less for both, which will give you some space to add labels to your graph.  The other two variables are pixel_x_offset_of_graph and pixel_y_offset_of_graph , which control the offset of the graph in pixels on the screen.  If you are adding the 20 pixel space I suggested, you may want to set the offset values to 20 each.

So, this next part is going to get a little advanced, so I'll try my best to explain everything as I go along.

Now that we have a formula, we're probably going to want to put that into a function.  This means we are optimizing your code in two ways: First, less typing, you can call a simple function, rather than typing out the whole formula every time.  Second, it means that if there is a bug in the formula, we only need to fix it once, instead of everywhere it's been typed.

So, let's give this a try, and we'll define two new functions in QBasic for this:

DECLARE FUNCTION plot_graph_x( BYVAL x_value AS SINGLE, BYVAL lowest_x_value AS SINGLE, BYVAL heighest_x_value, BYVAL pixel_width_of_graph AS INTEGER, BYVAL pixel_x_offset_of_graph AS INTEGER ) AS INTEGER
DECLARE FUNCTION plot_graph_y( BYVAL y_value AS SINGLE, BYVAL lowest_y_value AS SINGLE, BYVAL heighest_y_value, BYVAL pixel_height_of_graph AS INTEGER, BYVAL pixel_y_offset_of_graph AS INTEGER ) AS INTEGER

After declaring a function in QBasic, you can begin writing code in it by selecting the function from the menu.  I don't have a copy of QBasic with me right now, but I believe pressing F2 will allow you to select the function you want, and it will bring you to a fresh window with the FUNCTION code.  You can write the function anywhere in-between the FUNCTION function_name( params ) and END FUNCTION

So, plot_graph_x will look like this:

FUNCTION plot_graph_x( BYVAL x_value AS SINGLE, BYVAL lowest_x_value AS SINGLE, BYVAL heighest_x_value, BYVAL pixel_width_of_graph AS INTEGER, BYVAL pixel_x_offset_of_graph AS INTEGER ) AS INTEGER
 FUNCTION = CINT( ( ( (x_value - lowest_x_value) / (highest_x_value - lowest_x_value) ) * CSNG( pixel_width_of_graph ) ) + CSNG( pixel_x_offset_of_graph ) )
END FUNCTION

Notice how I wrapped the formula in CINT(), which converts the whole formula to an INTEGER, so QBasic's graphic commands can understand the value we are giving it better.

The plot_graph_y is set up the same way:

FUNCTION plot_graph_y( BYVAL y_value AS SINGLE, BYVAL lowest_y_value AS SINGLE, BYVAL heighest_y_value, BYVAL pixel_height_of_graph AS INTEGER, BYVAL pixel_y_offset_of_graph AS INTEGER ) AS INTEGER
 FUNCTION = CINT( ( ( (y_value - lowest_y_value) / (highest_y_value - lowest_y_value) ) * CSNG( -pixel_height_of_graph ) ) + CSNG( pixel_y_offset_of_graph ) )
END FUNCTION

Notice how we're using the negative value of the pixel_heght_of_graph - this is because QBasic actually implements it's graphics on a flipped y axis, in comparison to most other graphics utilities in programming.

So now that we have our basic functions made, we can start setting up our main body of the program:

SCREEN 12

'' Our ranges are between 0.4 and 1.0 for y, and between 0.435 and 0.7682 for x
'' Using ! as a suffix in our variable name means we want this variable to be a SINGLE
CONST x_min! = 0.435
CONST x_max! = 0.7682
CONST y_min! = 0.4
CONST y_max! = 1.0

'' Using % as a suffix in our variable name means we want this variable to be an INTEGER
CONST graph_width% = 620 '' 640 - 20
CONST graph_height% = 460 '' 480 - 20
CONST graph_x% = 20 '' X offset of graph from the left
CONST graph_y% = 460 '' Y offset of graph from the top

'' First, clear the screen, since DOS does not always clean itself up properly
CLS

'' Now, let's draw our graphing area
LINE (graph_x%, graph_y%)-STEP(0, -graph_height%), 15 '' Draw a white (15) line along the y axis, notice -graph_height so we draw up
LINE (graph_x%, graph_y%)-STEP(graph_width%, 0), 15 '' Draw a white (15) line along the x axis

'' Try plotting a point
PSET ( plot_graph_x( 0.435, x_min!, x_max!, graph_width%, graph_x% ), plot_graph_y( 0.758, y_min!, y_max!, graph_height%, graph_y% ), 4 '' If I recall correctly, 4 is red


This can now be the basis for your ability to draw your graph.  Since I suspect this may be a homework question, I can't give you any more of an answer, as I've already given you a large advantage.  Let me know if you have questions about the code I presented.  I am not currently able to test the code, so there may be some minor bugs.

If something doesn't work, please attach your *.bas file, and I can review the problems that you are having, but I will not be able to modify/update your code directly if this is a homework question.

I hope this helps,
-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.