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

0 Members and 1 Guest are viewing this topic.

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 #15 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.

Offline robpugh0829

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

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 #17 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?


Offline robpugh0829

  • Newbie
  • Posts: 19
    • View Profile
Re: Need help with trying to draw a circle with the mouse
« Reply #18 on: October 04, 2018, 08:06:25 am »
I am, your code is very clean and easy to understand. Thanks again!