Detect if user clicks inside a circle
A circle, is the geometric position of all the points whose distance from a central point is equal to some number "R".
You want to find the points whose distance is less than or equal to that "R", our radius.
The distance equation in 2d euclidean space is d(p1,p2) = root((p1.x-p2.x)^2 + (p1.y-p2.y)^2)
.
Check if the distance between your p
and the center of the circle is less than the radius.
Let's say I have a circle with radius r
and center at position (x0,y0)
and a point (x1,y1)
and I want to check if that point is in the circle or not.
I'd need to check if d((x0,y0),(x1,y1)) < r
which translates to:
Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)) < r
In JavaScript.
Now you know all these values (x0,y0)
being bubble.x
and bubble.y
and (x1,y1)
being x
and y
.
How to detect whether or not user is clicking inside Circle in python graphics window
You already have the centre and radius of each Circle
, you could write a function to determine whether a given Point
is within that as follows:
from math import sqrt
def is_within(point, circle):
distance = sqrt(((point.x - circle.x) ** 2) +
((point.y - circle.y) ** 2))
return distance < circle.radius
Note that you will have to tweak the attribute names according to the graphics library you are using.
Canvas circle detect click position
Check out source of jPlayer circle demo.
You can use the same approach to get percentage:
var x = e.pageX - canvas.offsetLeft - w/2,
y = e.pageY - canvas.offsetTop - h/2,
mAngle = Math.atan2(y, x);
if (mAngle > -1 * Math.PI && mAngle < -0.5 * Math.PI) {
mAngle = 2 * Math.PI + mAngle;
}
var percentage = (mAngle + Math.PI / 2) / 2 * Math.PI * 10;
demo: http://jsfiddle.net/7RUt3/
How do I check to see if a mouse click is within a circle in pygame?
Use the distance formula:
################################################################################
# Imports ######################################################################
################################################################################
from pygame.locals import *
import pygame, sys, math
################################################################################
# Screen Setup #################################################################
################################################################################
pygame.init()
scr = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Box Test')
################################################################################
# Game Loop ####################################################################
################################################################################
while True:
pygame.display.update(); scr.fill((200, 200, 255))
pygame.draw.circle(scr, (0, 0, 0), (400, 300), 100)
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
sqx = (x - 400)**2
sqy = (y - 300)**2
if math.sqrt(sqx + sqy) < 100:
print 'inside'
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
################################################################################
################################################################################
################################################################################
Getting the circle position on where the user clicks?
Your draw_circle
function is doing two separate things, so I'm not sure it makes sense to keep it as a single function. It's not updating the circle's x and y coordinates correctly because you're always returning random coordinates even when you should be returning the moved circle's coordinates. I've broken out the two parts of that function into the main circle_game
function in my code below.
I've replaced your check for a click inside a square with a check for a click inside a circle: (mouse.x - circle_x ) ** 2 + (mouse.y - circle_y) ** 2 <= CIRCLE_RADIUS_SQUARED
.
I also have no idea what totalClicks += random.randint(0,5)
was trying to achieve.
import random
from graphics import *
def circle_game():
CANVAS_WIDTH = 600
CANVAS_HEIGHT = 600
CIRCLE_RADIUS = 50
CIRCLE_RADIUS_SQUARED = CIRCLE_RADIUS * CIRCLE_RADIUS
window = GraphWin("Click circles", CANVAS_WIDTH, CANVAS_HEIGHT)
# Draw circle at random location to start game
circle_x = random.randint(0, CANVAS_WIDTH)
circle_y = random.randint(0, CANVAS_HEIGHT)
circle_point = Point(circle_x, circle_y)
circle = Circle(circle_point, CIRCLE_RADIUS)
circle.setFill(color_rgb(200, 0, 0))
circle.draw(window)
total_clicks = 0
inside_clicks = 0
while total_clicks < 10 and inside_clicks < 3:
mouse = window.getMouse()
total_clicks += 1
if (mouse.x - circle_x ) ** 2 + (mouse.y - circle_y) ** 2 <= CIRCLE_RADIUS_SQUARED:
# Click was inside circle so we don't move circle
inside_clicks +=1
else:
# Click was not inside circle so we move circle to mouse location
dx = mouse.x - circle_x
dy = mouse.y - circle_y
circle.move(dx, dy)
circle_x += dx
circle_y += dy
if __name__ == '__main__':
circle_game()
Detect mouse click on circle that is on different surface
You have to compute the mouse position relative to the Surface.
You have 2 possibilities. Either subtract the (top left) position of the Surface on the screen from the mouse position:
x = event.pos[0] - s_rect.left
y = event.pos[1] - s_rect.top
Or add the (top left) position of the Surface on the screen to the center point of the circle:
sqx = (x - (s_rect.left + 20))**2
sqy = (y - (s_rect.top + 20))**2
Complete example
from pygame.locals import *
import pygame, math
pygame.init()
screen = pygame.display.set_mode((640, 480))
s = pygame.Surface((100, 100))
s.fill((255, 0, 0))
s_rect = s.get_rect(center = (300, 300))
clicked = False
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x = event.pos[0]
y = event.pos[1]
sqx = (x - (s_rect.left + 20))**2
sqy = (y - (s_rect.top + 20))**2
if math.sqrt(sqx + sqy) < 20:
clicked = not clicked
print ('inside')
screen.fill((200, 200, 255))
color = (255, 255, 255) if clicked else (0, 0, 0)
pygame.draw.circle(s, color, (20, 20), 20)
screen.blit(s, s_rect)
pygame.display.update()
pygame.quit()
Detect if an circle is clicked in pygame?
You need to calculate the Euclidean distance between the center of the circle and the mouse. Test if this distance is less than the radius:
def clicked(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
dx = mouse_x - self.circle_x
dy = mouse_y - self.circle_y
distance = math.sqrt(dx*dx + dy*dy) # or: distance = math.hypot(dx, dy)
return distance <= self.circle_radius
You can improve performance and get rid of the costly math.sqrt
operation by comparing the square of the distance to the square of the radius:
def clicked(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
dx = mouse_x - self.circle_x
dy = mouse_y - self.circle_y
return dx*dx + dy*dy <= self.circle_radius*self.circle_radius
Related Topics
Is There a Good JavaScript Minifier
Getting Blob Data from Xhr Request
JavaScript Parser in JavaScript
Count Number of Matches of a Regex in JavaScript
Javascript/Jquery: Test If Window Has Focus
Detect When an Image Fails to Load in JavaScript
JavaScript Sort Function. Sort by First Then by Second
How to Insert a Character After Every N Characters in JavaScript
Date VS New Date in JavaScript
Rails 4 Turbo-Link Prevents Jquery Scripts from Working
How to Prevent Default Event Handling in an Onclick Method
How to Use Google.Maps.Event.Trigger(Map, 'Resize')
Decode HTML Entities in JavaScript
Writing File to Desktop Using HTML5 Filesystem API
Detect Browser Wrapped Lines via JavaScript
What Tag in HTML Should I Use Without Special Meaning, Only to Carry Meta Data