Author Topic: formula to determine if point is inside a circle ?  (Read 4684 times)

0 Members and 1 Guest are viewing this topic.

Offline mynameispaul

  • Newbie
  • Posts: 49
    • View Profile
formula to determine if point is inside a circle ?
« on: October 29, 2020, 09:11:08 am »
does someone have a formula that can determine if a point is within a circle if the circle's X Y and radius is known?
thanks

FellippeHeitor

  • Guest
Re: formula to determine if point is inside a circle ?
« Reply #1 on: October 29, 2020, 09:18:29 am »
Code: QB64: [Select]
  1. 'Calculate the distance between two points.
  2. FUNCTION dist! (x1!, y1!, x2!, y2!)
  3.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))

If dist!(x of circle center, y of circle center, x of your point, y of your point) is =< radius, your point is within the circle.

FellippeHeitor

  • Guest
Re: formula to determine if point is inside a circle ?
« Reply #2 on: October 29, 2020, 09:22:33 am »
Code: QB64: [Select]
  1. TYPE vector
  2.     x AS SINGLE
  3.     y AS SINGLE
  4.     r AS INTEGER
  5.  
  6. DIM circ AS vector
  7. DIM pt AS vector
  8.  
  9. SCREEN _NEWIMAGE(600, 600, 32)
  10.  
  11. circ.x = _WIDTH / 2
  12. circ.y = _HEIGHT / 2
  13. circ.r = 100
  14.  
  15.     pt.x = _MOUSEX
  16.     pt.y = _MOUSEY
  17.  
  18.     CLS
  19.     CIRCLE (circ.x, circ.y), circ.r, _RGB32(255)
  20.  
  21.     IF dist(circ.x, circ.y, pt.x, pt.y) <= circ.r THEN
  22.         PAINT (circ.x, circ.y), _RGB32(0, 116, 0), _RGB32(255)
  23.     END IF
  24.  
  25.     _DISPLAY
  26.     _LIMIT 60
  27.  
  28. 'Calculate the distance between two points.
  29. FUNCTION dist! (x1!, y1!, x2!, y2!)
  30.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))
  31.  

Offline qbkiller101

  • Newbie
  • Posts: 73
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #3 on: October 29, 2020, 09:24:18 am »
we were just discussing it on the discord server, just before you asked,
anyways, heres the formula (returns true if inside circle):
((pointX-circleX)^2 + (pointY - circleY)^2 < r^2)
where pointx and y are x,y of the point, and circle x,y are the circle's center's x and y coords

Offline mynameispaul

  • Newbie
  • Posts: 49
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #4 on: October 29, 2020, 10:22:55 am »
Thanks all.
Appreciate it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #5 on: October 29, 2020, 11:27:48 am »
Take Fellippe's
Code: QB64: [Select]
  1. IF dist(circ.x, circ.y, pt.x, pt.y) <= circ.r THEN

 and make collision of 2 circles
Code: QB64: [Select]
  1. IF dist(circ1.x, circ1.y, circ2.x, circ2.y) <= circ1.r + circ2.r THEN 'collision!

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #6 on: November 02, 2020, 08:26:44 pm »
Good evening everyone,
Taking advantage of the post, not to open another, with a simple request for help.

As everyone knows, I am a simple qbasic apprentice.
I am having trouble associating a number in its quantities.
Example:
a (1) = 2
a (2) = 3
a (3) = 5
a (4) = 7
a (5) = 11
a (6) = 13
a (7) = 17
a (8) = 19
a (9) = 23
a1 (a (1)) = 1250
a1 (a (2)) = 1262
a1 (a (3)) = 1245
a1 (a (4)) = 1212
a1 (a (5)) = 1266
a1 (a (6)) = 1282
a1 (a (7)) = 1238
a1 (a (8)) = 1248
a1 (a (9)) = 1245

When I order in quantities 1282 is the first that corresponds to the number 13. The problem is that I am not able to associate 1282 with 13.
Follows small code and image.

DIM a(1 TO 9) AS LONG
DIM a1(500) AS _INTEGER64
'-----------------------------------
a(1) = 2
a(2) = 3
a(3) = 5
a(4) = 7
a(5) = 11
a(6) = 13
a(7) = 17
a(8) = 19
a(9) = 23
'-----------------------------------
a1(a(1)) = 1250
a1(a(2)) = 1262
a1(a(3)) = 1245
a1(a(4)) = 1212
a1(a(5)) = 1266
a1(a(6)) = 1282
a1(a(7)) = 1238
a1(a(8)) = 1248
a1(a(9)) = 1245
'-----------------------------------
v = 2

FOR b = 1 TO 9
    LOCATE v, 2: PRINT a(b); "="; a1(a(b))
    v = v + 1
NEXT b

FOR b = 1 TO 9
    FOR i = 1 TO 8
        IF a1(a(i)) > a1(a(i + 1)) THEN
            SWAP a1(a(i)), a1(a(i + 1))
        END IF
    NEXT i
NEXT b
v = 2
FOR b = 9 TO 1 STEP -1
    LOCATE v, 20: PRINT a(b); "="; a1(a(b))
    v = v + 1
NEXT b

Carlos
Associar.jpg
* Associar.jpg (Filesize: 140.78 KB, Dimensions: 868x737, Views: 188)
« Last Edit: November 02, 2020, 09:15:04 pm by carloscordeiro »

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #7 on: November 03, 2020, 06:11:43 pm »
Bplus Didn't you wake up funny today to help me with this?

After SWAP it loses association.
I didn't mention your name, because sometimes I'm ashamed to ask you directly. considering that you and steve already help me a lot in this forum.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #8 on: November 03, 2020, 06:37:01 pm »
Wasn't there another post on this topic sometime last year?  Or am I just having deja vu?
Granted after becoming radioactive I only have a half-life!

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #9 on: November 03, 2020, 06:45:56 pm »
Not with me

I'm taking advantage of the post, not to open another with a simple code
Did you understand what I'm up to?

I want to put in order by bigger numbers, and the associated numbers follow the order

Before the SWAP command the lines are associated, after the SWAP it keeps the larger numbers, however it disassociates.
« Last Edit: November 03, 2020, 06:51:55 pm by carloscordeiro »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #10 on: November 04, 2020, 10:35:13 am »
Bplus Didn't you wake up funny today to help me with this?

After SWAP it loses association.
I didn't mention your name, because sometimes I'm ashamed to ask you directly. considering that you and steve already help me a lot in this forum.

I'm sorry Carlos I haven't a clue what you are up to here.

Also if you have your own question unrelated to discussion or code, it is perfectly fine and courteous to start a new post so don't worry about starting a new topic please. Or you are free to PM me. :)

And also if it is more a question than something in particular you are doing in code it should be started under Discussion board.

So see, if you had started your own discussion I would feel free to pester you with questions like what the heck are you trying to do? Fortunately mynameispaul seemed finished and topic did seem done so...

I see a() is first 1 to 9 primes and then a1() array you assign the first 9 prime indexes in 1200's, why would anybody do that? ;-))  so a1(2) = 1250   a1(3) = 1262 and so on...

And then you do some crazy thing with swapping, why would anybody do that? ;-))

And so my friend as I said, I have no idea what you are up to. :)



Update:

Quote
I want to put in order by bigger numbers, and the associated numbers follow the order

Before the SWAP command the lines are associated, after the SWAP it keeps the larger numbers, however it disassociates.

OK got it!
Code: QB64: [Select]
  1. REDIM a(0 TO 1, 1 TO 9)
  2.  
  3. 'load a() with primes in 0 index and 1200's in  index 1, now the prime number is bonded to the 1200 number
  4. a(0, 1) = 2
  5. a(0, 2) = 3
  6. a(0, 3) = 5
  7. a(0, 4) = 7
  8. a(0, 5) = 11
  9. a(0, 6) = 13
  10. a(0, 7) = 17
  11. a(0, 8) = 19
  12. a(0, 9) = 23
  13.  
  14. a(1, 1) = 1250
  15. a(1, 2) = 1262
  16. a(1, 3) = 1245
  17. a(1, 4) = 1212
  18. a(1, 5) = 1266
  19. a(1, 6) = 1282
  20. a(1, 7) = 1238
  21. a(1, 8) = 1248
  22. a(1, 9) = 1245
  23.  
  24. ' now sort a() according to index 1 not 0
  25.  
  26. FOR i = 1 TO 8
  27.     FOR j = i + 1 TO 9
  28.         IF a(1, i) > a(1, j) THEN
  29.             SWAP a(0, i), a(0, j) ' when swap a 1200 number swap primes of same index
  30.             SWAP a(1, i), a(1, j)
  31.         END IF
  32.     NEXT
  33.  
  34. FOR i = 1 TO 9
  35.     PRINT a(0, i), a(1, i)
  36.  
« Last Edit: November 04, 2020, 12:01:50 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #11 on: November 04, 2020, 11:35:01 am »
Hi Carloscodeiro. You say you're an apprentice. So first learn to open new topics where we can focus on you. How does mynameispaul come to this when his thread, which he apparently follows, must solves your problem? Would you like that? It is impolite.

Your problem is here:

Code: QB64: [Select]
  1. DIM a (19) AS LONG
  2. DIM a1 (500) AS _INTEGER64
  3. '--------------------------------- -
  4. a (1) = 2
  5. a (2) = 3
  6. a (3) = 5
  7. a (4) = 7
  8. a (5) = 11
  9. a (6) = 13
  10. a (7) = 17
  11. a (8) = 19
  12. a (9) = 23
  13. '-----------------------------------
  14. a1 (a (1)) = 1250
  15. a1 (a (2)) = 1262
  16. a1 (a (3)) = 1245
  17. a1 (a (4)) = 1212
  18. a1 (a (5)) = 1266
  19. a1 (a (6)) = 1282
  20. a1 (a ( 7)) = 1238
  21. a1 (a (8)) = 1248
  22. a1 (a (9)) = 1245
  23.  

  a1 (a(1)) ----- is the same as a1 (2) because a(1) is 2.

easy work:

For S = 1 to 9
SWAP A1(s), A(s)   'this replace values between A1 and A arrays.
NEXT S

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #12 on: November 04, 2020, 11:49:43 am »
mynameispaul: Copyed from CIRCLE help + modified to function:

Code: QB64: [Select]
  1.  
  2. 'original source code
  3. PRINT "Use mouse, then press Esc for function test..."
  4. r& = 200 'radius    change circle size and position here
  5. cx& = 320 'center x horizontal
  6. cy& = 240 'center y vertical
  7.  
  8.     i = _MOUSEINPUT
  9.     x& = _MOUSEX
  10.     y& = _MOUSEY
  11.     xy& = ((x& - cx&) ^ 2) + ((y& - cy&) ^ 2) 'Pythagorean theorem
  12.     IF r& ^ 2 >= xy& THEN CIRCLE (cx&, cy&), r&, 10 ELSE CIRCLE (cx&, cy&), r&, 12
  13. LOOP UNTIL INKEY$ = CHR$(27) 'escape key exit
  14.  
  15. 'modified to function:
  16.     x& = _MOUSEX
  17.     y& = _MOUSEY
  18.     'cx&, cy&, r& are the same as is begin
  19.     LOCATE 2
  20.     IF INCIRCLE(x&, y&, r&, cx&, cy&) THEN PRINT "Point is in circle    " ELSE PRINT "Point is not in circle"
  21.  
  22.  
  23. FUNCTION INCIRCLE (x&, y&, r&, cx&, cy&)
  24.     xy& = ((x& - cx&) ^ 2) + ((y& - cy&) ^ 2) 'Pythagorean theorem
  25.     IF r& ^ 2 >= xy& THEN INCIRCLE = 1 ELSE INCIRCLE = 0
  26.  
  27.  

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: formula to determine if point is inside a circle ?
« Reply #13 on: November 04, 2020, 05:38:46 pm »
I apologize amynameispaul and everyone who understood this. I had no bad intention of reopening the topic and I also had no intention of being rude.

Bplus, you left me more relaxed. When I need to open topics, I will no longer be embarrassed by my learner questions.

In my mind, in asking a question directly to you Bplus, I thought I was belittling others in the forum.

Petr, you are right I was rude, but as I mentioned, it was not my intention.

I will try your suggestion for SWAP A1 (s), A (s) If it does not solve I will open a topic explaining better.

This ear tug is another learning experience for me.