Author Topic: Need help with trying to draw a circle with the mouse  (Read 6601 times)

0 Members and 1 Guest are viewing this topic.

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Need help with trying to draw a circle with the mouse
« 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!
« Last Edit: October 02, 2018, 11:14:45 pm by odin »

FellippeHeitor

  • Guest
Re: Need help with trying to draw a circle with the mouse
« Reply #1 on: October 02, 2018, 10:28:07 pm »
Please post your current attempt.

PS: Welcome to the Forum.
« Last Edit: October 02, 2018, 11:14:52 pm by odin »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #2 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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Need help with trying to draw a circle with the mouse
« Reply #3 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.
« Last Edit: October 03, 2018, 01:55:27 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #4 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.
In order to understand recursion, one must first understand recursion.

FellippeHeitor

  • Guest
Re: Need help with trying to draw a circle with the mouse
« Reply #5 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.

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #6 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

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #7 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!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Need help with trying to draw a circle with the mouse
« Reply #8 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).
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #9 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!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #10 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.

« Last Edit: October 03, 2018, 09:54:47 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Need help with trying to draw a circle with the mouse
« Reply #11 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.   ;)

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #12 on: October 03, 2018, 10:11:52 am »
;-)) runs fine until you click the mouse!

FellippeHeitor

  • Guest
Re: Need help with trying to draw a circle with the mouse
« Reply #13 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" 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
« Last Edit: October 03, 2018, 10:16:48 am by FellippeHeitor »

Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #14 on: October 03, 2018, 12:09:44 pm »
BPlus,
I'm using Dav's IDE.

Everybody,
Thanks again for all the help and answers!