Squares??

New to FreeBASIC? Post your questions here.
Post Reply
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

Squares??

Post by HillbillyGeek »

Before I ask this let me throw this out there: I'm not totally sure how to exactly ask what it is I'm asking......please be aware of that when answering. I tend to like reading/writing very small programs that allow me to learn concepts etc. in small chunks (I don't think I'm alone in that regard).

I've been assisted with and have written several circle-drawing bits etc etc

What about drawing squares (more than one on-screen)? Would you load these as graphics (PNG.....? BMP......?) or would you literally draw these via lines?

Any small example(s) maybe??

Thanks!!
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Squares??

Post by TESLACOIL »

Code: Select all

screen 19 
line (50,50)-(100,120),9,b
sleep 500
line (50,50)-(100,120),9,bf
sleep 500
bload "testimage.bmp" 'assuming you have one
sleep:end
I use two for next loops to create a chess board x and y values, with separate counters for colors and resetting x and y values

I also use ox and oy as offset variables. ox = 50 :oy=50 , so that the chess board doesn't start at the very edge of the screen


You can end up with some long winded line statements but it works, figure out a naming system your happy with. Im lazy so i use the following.

Code: Select all

dim as integer i,ii,iii,x,xx,ox,y,yy,ox,c,col
loop counters, graphical counters, colour counters

Code: Select all

line (i,ii+99)-(i-(hmap(i,ii)/40),ii-(hmap(i,ii)/9)+99),hmap(i,ii)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Squares??

Post by BasicCoder2 »

HillbillyGeek wrote:What about drawing squares (more than one on-screen)? Would you load these as graphics (PNG.....? BMP......?) or would you literally draw these via lines?
What is your "squares" project?
How you do something depends on what you are trying to achieve, there is no single answer.
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

Re: Squares??

Post by HillbillyGeek »

A perfectly fair response. My goal for output is:

Draw (insert, create.....?) two colored squares, each measuring about 2 in X 2 in side-by-side in the center of a black screen.

That's it.....nothing else.
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

Re: Squares??

Post by HillbillyGeek »

...and along those lines (trying to show that I'm attempting to answer my own question here):

I'm thinking PAINT or PUT might factor in there somewhere....?
gothon
Posts: 225
Joined: Apr 11, 2011 22:22

Re: Squares??

Post by gothon »

With the FreeBASIC graphics you can easily draw axis aligned squares (and rectangles), by passing the quirk parameter 'B' (which stands for 'box') to the line command. You can even have a filled square or box by passing 'BF'.

Code: Select all

ScreenRes 640, 480, 32

Line (100, 100)-(200, 200), , B
Line (250, 100)-(350, 200), , BF

Sleep
KeyPgLinegraphics

The general way to draw an arbitrarily oriented square is to determine the 4 corner points and draw the 4 lines yourself.

Ex:

Code: Select all

'Square of color C, width W, centered at (X, Y), rotated by angle A
Sub Square(X As Single, Y As Single, W As Single, A As Single, C As ULong)
    Var RX = Cos(A+Atn(1))*W*Sqr(2)/2 'Note: Atn(1) = Pi/4
    Var RY = Sin(A+Atn(1))*W*Sqr(2)/2
    
    Line (X - RX, Y - RY)-(X - RY, Y + RX), C
    Line (X - RY, Y + RX)-(X + RX, Y + RY), C
    Line (X + RX, Y + RY)-(X + RY, Y - RX), C
    Line (X + RY, Y - RX)-(X - RX, Y - RY), C
End Sub

ScreenRes 640, 480, 32

For I As Integer = 1 To 16
    Var C = RGB(255*Rnd, 255*Rnd, 255*Rnd)
    Var X = 50 + 400*Rnd
    Var Y = 50 + 400*Rnd
    Var W = 20 + 100*Rnd
    Var A = 4*Atn(1)*Rnd
    
    Square X, Y, W, A, C
    Circle (X, Y), W/2, C, 8*Atn(1) - A,  6*Atn(1) - A
Next I

Sleep
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Squares??

Post by BasicCoder2 »

In fact your last example does not use the coordinates of the four corners as parameters it uses the length of the sides W and the center of rotation X,Y. Also the user needs to know that A is in radians.
Such subs can be used without understanding or given with the assumption the person is a mathematician and can figure it out.

Code: Select all

'some useful defines
Const Pi = 4 * Atn(1)
Dim Shared As Double TwoPi = 8 * Atn(1)
Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radians

'Square of color C, width W, centered at (X, Y), rotated by angle A
Sub Square(X As Single, Y As Single, W As Single, A As Single, C As ULong)
    Var RX = Cos(A+Atn(1))*W*Sqr(2)/2 'Note: Atn(1) = Pi/4
    Var RY = Sin(A+Atn(1))*W*Sqr(2)/2
    Line (X - RX, Y - RY)-(X - RY, Y + RX), C
    Line (X - RY, Y + RX)-(X + RX, Y + RY), C
    Line (X + RX, Y + RY)-(X + RY, Y - RX), C
    Line (X + RY, Y - RX)-(X - RX, Y - RY), C
    circle (X,Y),5,rgb(255,255,0),,,,f
    line (X,Y)-(X-RX,Y-RY),rgb(0,0,255)
End Sub

ScreenRes 640, 480, 32

dim as ULONG C
dim as SINGLE X,Y,W,A

for angle as single = 0 to 360 step 10
    cls
    locate 2,2
    print "tap esc key to rotate square" 
    C = rgb(255,0,0)
    X = 100  'center of rotation
    Y = 100
    W = 100  'length of sides
    A = angle * DtoR 'degrees to radians

    Square X, Y, W, A, C
    sleep
next angle
Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares??

Post by dodicat »

Rather than a box centre + angle to determine the orientation, you could use a line then draw the box round the line:

Code: Select all


Sub boxfill(x1 As Single,y1 As Single,x2 As Single,y2 As Single,col As Uinteger,w as single=1)
    Dim As Single dx=(x2-x1)/w,dy=(y2-y1)/w
    Swap dx,dy:dx=-dx
    Dim As Single p1x=x1+dx/2,p1y=y1+dy/2
    Dim As Single p2x=x1-dx/2,p2y=y1-dy/2
    Dim As Single p3x=x2+dx/2,p3y=y2+dy/2
    Dim As Single p4x=x2-dx/2,p4y=y2-dy/2
    Dim As Uinteger c=Rgb(255,255,254)
    For x As Integer=1 To 2
        line(p1x,p1y)-(p2x,p2y),c
        Line(p3x,p3y)-(p4x,p4y),c
        Line(p1x,p1y)-(p3x,p3y),c
        Line(p2x,p2y)-(p4x,p4y),c
        Paint((p1x+p2x+p3x+p4x)/4,(p1y+p2y+p3y+p4y)/4),c,c
        c=col
    Next x
End Sub

screen 19,32
windowtitle "Mouse and wheel"
dim as integer mx,my,mw
do
    getmouse mx,my,mw
    screenlock
    cls
    locate 1,1
    print "ratio of box length to width "; 1+mw/10
boxfill(400,300,mx,my,rgb(0,200,0),1+mw/10)
line(400,300)-(mx,my)
screenunlock
sleep 1,1
loop until len(inkey)

sleep
 
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

Re: Squares??

Post by HillbillyGeek »

Stellar examples, all the way around: thank you all!

I may be trying to get WAY OUT THERE now......but how might one use PUT or PAINT herein? Or would it even be worth it?
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Re: Squares??

Post by rolliebollocks »

Short answer is no. Paint is never worth using because it has a tendency to color outside the lines. There are better ways to fill polygons and squares using line with BF switch fill themselves. Put is good for complex graphics and will come in handy for you eventually, but it won't be because you're drawing squares.

If all you're doing is experimenting then PUT is definitely something you want to have a good command of.

There is also MULTIPUT http://www.freebasic.net/forum/viewtopi ... t=multiput which does a fantastic job of scaling and rotating images.

You might also be interested in looking into Cairo or OpenGL depending on what sort of development you're looking to do. Good luck.
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

Re: Squares??

Post by HillbillyGeek »

Code: Select all

ScreenRes 640, 480, 32

Line (100, 100)-(200, 200), , B
Line (250, 100)-(350, 200), , BF

Sleep
In the above example, why are the squares White? What if I wanted to modify their color(s)????
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Squares??

Post by dkl »

With a 24 or 32 bit screen mode, you can use rgb() colors, for example:

Code: Select all

ScreenRes 640, 480, 32

Line (100, 100)-(200, 200), rgb(255,0,0), B
Line (250, 100)-(350, 200), rgb(0,0,255), BF

Sleep
I suppose white is the default color, used if nothing else is given.

FB can also do alpha blending in 32bit mode, if enabled:

Code: Select all

#include once "fbgfx.bi"

ScreenRes 640, 480, 32, , fb.GFX_ALPHA_PRIMITIVES

Line (100, 100)-(200, 200), rgba(255,0,0,255), BF
Line (150, 150)-(250, 250), rgba(0,0,255,100), BF

Sleep
Other screen modes with lower bits per pixel use palette-based colors. There's a default palette with colors as in QB, though it's also possible to change the palette.
Post Reply