QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: robpugh0829 on October 02, 2018, 10:16:43 pm

Title: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 02, 2018, 10:16:43 pm
Hey all,
I am stuck and can't figure something out. If any of you could help me I would appreciate it. I'm trying to draw a circle with the mouse just like MS Paint does, how it chooses location and size using the mouse, etc... I can draw a circle with the mouse, but the sizing doesn't work and it just never is right for some reason, can somebody help me please?

Thanks!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: FellippeHeitor on October 02, 2018, 10:28:07 pm
Please post your current attempt.

PS: Welcome to the Forum.
Title: Re: Need help with trying to draw a circle with the mouse
Post by: bplus on October 03, 2018, 12:41:47 am
Well this draws ellipse like Paint but I wouldn't use either for important drawings. I would use QB64 to get the center and radii exactly how I want.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. _SCREENMOVE 300, 60
  3. bk& = _NEWIMAGE(800, 600, 32)
  4. _PUTIMAGE , 0, bk&
  5. WHILE _KEYDOWN(27) = 0
  6.     CLS
  7.     _PUTIMAGE , bk&, 0
  8.     mb = _MOUSEBUTTON(1)
  9.     IF mb AND corner1F = 0 THEN
  10.         x1 = _MOUSEX: y1 = _MOUSEY: corner1F = 1
  11.         mb = 0
  12.     ELSEIF mb AND corner1F = 1 THEN '2nd corner
  13.         x2 = _MOUSEX: y2 = _MOUSEY
  14.         IF max(x1, x2) = x1 THEN lowx = x2: hix = x1 ELSE lowx = x1: hix = x2
  15.         IF max(y1, y2) = y1 THEN lowy = y2: hiy = y1 ELSE lowy = y1: hiy = y2
  16.         cx = (hix + lowx) / 2: cy = (hiy + lowy) / 2
  17.         xr = cx - lowx: yr = cy - lowy
  18.         ellipse cx, cy, xr, yr
  19.     ELSEIF mb = 0 AND corner1F = 1 THEN
  20.         x2 = _MOUSEX: y2 = _MOUSEY
  21.         IF max(x1, x2) = x1 THEN lowx = x2: hix = x1 ELSE lowx = x1: hix = x2
  22.         IF max(y1, y2) = y1 THEN lowy = y2: hiy = y1 ELSE lowy = y1: hiy = y2
  23.         cx = (hix + lowx) / 2: cy = (hiy + lowy) / 2
  24.         xr = cx - lowx: yr = cy - lowy
  25.         ellipse cx, cy, xr, yr
  26.         IF ABS(x1 - x2) > 3 OR ABS(y1 - y2) > 3 THEN _PUTIMAGE , 0, bk&
  27.         corner1F = 0
  28.         x1 = 0: x2 = 0: y1 = 0: y2 = 0
  29.     END IF
  30.     _DISPLAY
  31.     _LIMIT 60
  32.  
  33. SUB ellipse (CX AS LONG, CY AS LONG, xRadius AS LONG, yRadius AS LONG)
  34.     DIM scale AS SINGLE, xs AS LONG, x AS LONG, y AS LONG
  35.     DIM lastx AS LONG, lasty AS LONG
  36.     scale = yRadius / xRadius: xs = xRadius * xRadius
  37.     PSET (CX, CY - yRadius): PSET (CX, CY + yRadius)
  38.     lastx = 0: lasty = yRadius
  39.     FOR x = 1 TO xRadius
  40.         y = scale * SQR(xs - x * x)
  41.         LINE (CX + lastx, CY - lasty)-(CX + x, CY - y)
  42.         LINE (CX + lastx, CY + lasty)-(CX + x, CY + y)
  43.         LINE (CX - lastx, CY - lasty)-(CX - x, CY - y)
  44.         LINE (CX - lastx, CY + lasty)-(CX - x, CY + y)
  45.         lastx = x: lasty = y
  46.     NEXT
  47.  
  48. FUNCTION max (a, b)
  49.     IF b > a THEN max = b ELSE max = a
  50.  
Title: Re: Need help with trying to draw a circle with the mouse
Post by: SMcNeill on October 03, 2018, 01:46:34 am
SCREEN _NEWIMAGE(640.480,32)
Radius = 25
DO
    _LIMIT 60
    WHILE _MOUSEINPUT: Radius = Radius + _MOUSEWHEEL:WEND
    IF _MOUSEBUTTON(1) THEN
        CIRCLE (_MOUSEX, _MOUSEY), Radius, _RGB32(255,255,255)
     END IF
LOOP UNTIL _KEYHIT = 27

**************

Done via memory, on my iPad, so may require a little tweaking to correct syntax 100%.

Use left button to draw the circle, wheel to make size larger or smaller.
Title: Re: Need help with trying to draw a circle with the mouse
Post by: TerryRitchie on October 03, 2018, 02:00:54 am
School's back in session guys. Be careful, we might be getting homework requests. Always better to see what work has been done first and help them through their issues.  I had to watch my students like a hawk to make sure they weren't getting their work done 100% for them.
Title: Re: Need help with trying to draw a circle with the mouse
Post by: FellippeHeitor on October 03, 2018, 05:41:04 am
School's back in session guys. Be careful, we might be getting homework requests. Always better to see what work has been done first and help them through their issues.  I had to watch my students like a hawk to make sure they weren't getting their work done 100% for them.

I *always* try to check that first.

Please post your current attempt.

PS: Welcome to the Forum.


Bplus is *always* quick to give it ready first.
Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 03, 2018, 07:58:40 am
screen 12
do
   do while _MOUSEINPUT
      x = _mousex
      y = _mousey
      locate 1,1
      print x, y
      
      if _mousebutton(1) then
         circle(x,y),x + y / 2
      end if
   loop
loop
Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 03, 2018, 08:00:14 am
Thanks for all the answers, I will work through this. I'm coming back to QB after a long time doing other things such as Web Development with PHP and other languages. Sorry I didn't post my code first, I will remember that for next time. I'm too old to be getting my homework done ;-).

Thanks again guys!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: SMcNeill on October 03, 2018, 09:11:03 am
Two main issues I see here:

1) You're running inside the mouse loop, which can cause excessive program lag.  Your OS can poll the mouse thousands of times a second; PRINT generally won't keep up with it, and you'll get a bottleneck of events where it'll take the program several long seconds to catch up to mouse events even after you've quit interacting with it.

I'd suggest the following style changes:

DO WHILE _MOUSEINPUT: LOOP
X = _MOUSEX
Y = _MOUSEY
LOCATE 1,1
PRINT X, Y

2) The radius on your circle is inconsistent.  At the top left of the screen (X = 0, Y = 0), you're going to have a zero radius circle.  At the bottom of the screen, the radius is going to be larger than your whole screen, drawing off the map completely.  I'd suggest a fixed size radius, alterable by some other means.  (+/- keys for example to increase/decrease circle size).
Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 03, 2018, 09:25:45 am
SMcNeill,
Thanks! I was going to take the print away, I was just using that as a reference for where the pointer was at. I also do that when I'm building something with buttons and I need to know where the cursor is at to set the button press parameters in my if statement. I will also try what you said with the radius, that makes sense. Thanks a million!

Have a great day!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: bplus on October 03, 2018, 09:48:06 am
This didn't look like a homework assignment.

It is fun challenge to be first, hard to beat "Ninja fingers".

Steve there is typo in code you posted and IDE points not to cause of problem.

robpugh0829, your code didn't look like it came from QB64 IDE? What are you running from?

Try this: click down at your circle center, then drag out your radius, when just right, release mouse button and it becomes part of the permanent record.

Extra Bonus: Undo up to last 10 circles.

Title: Re: Need help with trying to draw a circle with the mouse
Post by: SMcNeill on October 03, 2018, 10:02:18 am
This didn't look like a homework assignment.

It is fun challenge to be first, hard to beat "Ninja fingers".

Steve there is typo in code you posted and IDE points not to cause of problem.

robpugh0829, your code didn't look like it came from QB64 IDE? What are you running from?

Try this: click down at your circle center, then drag out your radius, when just right, release mouse button and it becomes part of the permanent record.

Extra Bonus: Undo up to last 10 circles.

SCREEN _NEWIMAGE(640.480,32)

It's the period there, instead of a comma.  Oddly enough, it still runs and compiles just fine for me -- though the screensize is so large it runs completely off my monitor!  I guess it's making a 640x32 SCREEN 0 screen?

Just change that period to a comma and all is good.   ;)

Title: Re: Need help with trying to draw a circle with the mouse
Post by: bplus on October 03, 2018, 10:11:52 am
;-)) runs fine until you click the mouse!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: FellippeHeitor on October 03, 2018, 10:14:50 am
This didn't look like a homework assignment.

It is fun challenge to be first, hard to beat "Ninja fingers".

Not sure if naive or playing naive.

Better pay closer attention either way. Someone fishing for ninja fingers is someone who's likely being forced to go through a language course he/she hates and is not giving a shit about your skills. You'd be helping a cheater. Not encouraged.

Luckily it doesn't seem like it was robpug's case.

Here's my ninja code I had written last night after the initial post in this thread (my github repository "Snippets (https://github.com/FellippeHeitor/Snippets/blob/master/circleDraw.bas)" as proof of time). I didn't post it immediately trying to figure OP's good intentions first.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3. TYPE circles
  4.     x AS SINGLE
  5.     y AS SINGLE
  6.     radius AS SINGLE
  7.     ratio AS SINGLE
  8.  
  9. DIM circles(1 TO 1000) AS circles
  10.  
  11. _TITLE "Circle drawing"
  12.  
  13.  
  14.         IF NOT mouseDown THEN
  15.             mouseDown = -1
  16.             totalCircles = totalCircles + 1
  17.             IF totalCircles > 1000 THEN totalCircles = 1
  18.             mx1 = _MOUSEX
  19.             my1 = _MOUSEY
  20.             mx2 = mx1
  21.             my2 = my1
  22.             circles(totalCircles).color = _RGB32(RND * 255, RND * 255, RND * 255)
  23.         ELSE
  24.             mx2 = _MOUSEX
  25.             my2 = _MOUSEY
  26.         END IF
  27.     ELSE
  28.         mouseDown = false
  29.     END IF
  30.  
  31.     CLS
  32.  
  33.     IF mouseDown THEN
  34.         x1 = mx1
  35.         x2 = mx2
  36.         IF x1 > x2 THEN SWAP x1, x2
  37.  
  38.         y1 = my1
  39.         y2 = my2
  40.         IF y1 > y2 THEN SWAP y1, y2
  41.  
  42.         circles(totalCircles).x = x1 + (x2 - x1) / 2
  43.         circles(totalCircles).y = y1 + (y2 - y1) / 2
  44.         IF (x2 - x1) > (y2 - y1) THEN
  45.             circles(totalCircles).radius = (x2 - x1) / 2
  46.         ELSE
  47.             circles(totalCircles).radius = (y2 - y1) / 2
  48.         END IF
  49.         circles(totalCircles).ratio = (y2 - y1) / (x2 - x1)
  50.     END IF
  51.  
  52.     FOR i = 1 TO totalCircles
  53.         CIRCLE (circles(i).x, circles(i).y), circles(i).radius, circles(i).color, , , circles(i).ratio
  54.     NEXT
  55.  
  56.     _DISPLAY
  57.     _LIMIT 60
Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 03, 2018, 12:09:44 pm
BPlus,
I'm using Dav's IDE.

Everybody,
Thanks again for all the help and answers!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: bplus on October 03, 2018, 02:10:44 pm
As usual, everyone is done as B+ just getting warmed up. ;-))

Here is perfect circle drawing I was talking about PLUS unlimited Undo and Redo! PLUS if the circle formed is too close to the origin will assume user changed mind about drawing circle.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. _SCREENMOVE 300, 60
  3. bk& = _NEWIMAGE(800, 600, 32) 'back ground screen
  4. _PUTIMAGE , 0, bk& 'practice ;-))
  5. TYPE circleType 'for circle (x, y), r (black and white exercise)
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     r AS SINGLE
  9. REDIM cs(0) AS circleType 'circles array
  10. ci = 0 'stay ahead one of what has been recorded  for circle index
  11. WHILE _KEYDOWN(27) = 0
  12.     CLS
  13.     _PUTIMAGE , bk&, 0
  14.     k$ = INKEY$
  15.     IF k$ = "u" THEN 'undo
  16.         IF ci > 0 THEN ci = ci - 1
  17.         CLS
  18.         FOR i = 0 TO ci - 1
  19.             CIRCLE (cs(i).x, cs(i).y), cs(i).r
  20.         NEXT
  21.         _PUTIMAGE , 0, bk&
  22.     END IF
  23.     IF k$ = "r" THEN 'redo
  24.         IF ci < UBOUND(cs) THEN
  25.             ci = ci + 1
  26.             FOR i = 0 TO ci - 1
  27.                 CIRCLE (cs(i).x, cs(i).y), cs(i).r
  28.             NEXT
  29.             _PUTIMAGE , 0, bk&
  30.         END IF
  31.     END IF
  32.     mb = _MOUSEBUTTON(1)
  33.     IF mb AND md = 0 THEN
  34.         x1 = _MOUSEX: y1 = _MOUSEY: md = -1: mb = 0
  35.     ELSEIF mb AND md THEN 'draw sample circle
  36.         x2 = _MOUSEX: y2 = _MOUSEY
  37.         r = _HYPOT(x1 - x2, y1 - y2)
  38.         CIRCLE (x1, y1), r
  39.     ELSEIF mb = 0 AND md THEN
  40.         x2 = _MOUSEX: y2 = _MOUSEY
  41.         r = _HYPOT(x1 - x2, y1 - y2)
  42.         CIRCLE (x1, y1), r
  43.         IF ABS(x1 - x2) > 3 OR ABS(y1 - y2) > 3 THEN 'user may not want a circle here and goes back to x1, y1
  44.             _PUTIMAGE , 0, bk&
  45.             cs(ci).x = x1
  46.             cs(ci).y = y1
  47.             cs(ci).r = r
  48.             ci = ci + 1
  49.             REDIM _PRESERVE cs(ci) AS circleType
  50.         END IF
  51.         md = 0: x1 = 0: x2 = 0: y1 = 0: y2 = 0
  52.     END IF
  53.     _DISPLAY
  54.     _LIMIT 60
  55.  

I was very pleasantly surprised to find that _HYPOT could be looked up on-line from inside the IDE.

Hey this looks like the beginnings of a Paint program, mouse locations could help perfect drawings as well as displaying current radius.
Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 03, 2018, 03:23:53 pm
BPlus,
You are amazing! Thanks! I am working on making a paint program for my kids. They are growing up fast and I want them to have something that interests them and ms paint is boring, so I'm making them a themed paint program. It's going to be Avengers Themed, so obviously I would never be able to release it because of copyright and all, but it will be cool when it's done. Thanks again so much!
Title: Re: Need help with trying to draw a circle with the mouse
Post by: bplus on October 03, 2018, 09:43:05 pm
Good! You are seeing how to change CircleType to DrawType and doing lines and boxes... along with circles and color?

Title: Re: Need help with trying to draw a circle with the mouse
Post by: robpugh0829 on October 04, 2018, 08:06:25 am
I am, your code is very clean and easy to understand. Thanks again!