Detecting if an arc has been clicked in pygame
<p>I am currently trying to digitalize an boardgame I invented (repo: <a href="https://github.com/zutn/King_of_the_Hill" rel="nofollow">https://github.com/zutn/King_of_the_Hill</a>). To make it work I need to check if one of the tiles (the arcs) on this <a href="http://i.stack.imgur.com/DTXzF.jpg" rel="nofollow">board</a> have been clicked. So far I have not been able to figure a way without giving up the pygame.arc function for drawing. If I use the x,y position of the position clicked, I can't figure a way out to determine the exact outline of the arc to compare to. I thought about using a color check, but this would only tell me if any of the tiles have been clicked. So is there a convenient way to test if an arc has been clicked in pygame or do I have to use sprites or something completely different? Additionally in a later step units will be included, that are located on the tiles. This would make the solution with the angle calculation postet below much more diffcult. </p>

<p>This is a simple arc class that will detect if a point is contained in the arc, but it will only work with circular arcs.</p>

<pre><code>import pygame
from pygame.locals import *
import sys
from math import atan2, pi

class CircularArc:

    def __init__(self, color, center, radius, start_angle, stop_angle, width=1):
        self.color = color
        self.x = center[0]  # center x position
        self.y = center[1]  # center y position
        self.rect = [self.x - radius, self.y - radius, radius*2,  radius*2]
        self.radius = radius
        self.start_angle = start_angle
        self.stop_angle = stop_angle
        self.width = width




    def draw(self, canvas):
        pygame.draw.arc(canvas, self.color, self.rect, self.start_angle, self.stop_angle, self.width)



    def contains(self, x, y):

        dx = x - self.x   # x distance
        dy = y - self.y   # y distance

        greater_than_outside_radius = dx*dx + dy*dy &gt;= self.radius*self.radius

        less_than_inside_radius = dx*dx + dy*dy &lt;= (self.radius- self.width)*(self.radius- self.width)

        # Quickly check if the distance is within the right range
        if greater_than_outside_radius or less_than_inside_radius:
            return False



        rads = atan2(-dy, dx)  # Grab the angle

        # convert the angle to match up with pygame format. Negative angles don't work with pygame.draw.arc
        if rads &lt; 0:
            rads = 2 * pi + rads


        # Check if the angle is within the arc start and stop angles
        return self.start_angle &lt;= rads &lt;= self.stop_angle
</code></pre>

<p>Here's some example usage of the class. Using it requires a center point and radius instead of a rectangle for creating the arc.</p>

<pre><code>pygame.init()


black = ( 0, 0, 0)
width = 800
height = 800
screen = pygame.display.set_mode((width, height))


distance = 100
tile_num = 4
ring_width = 20

arc = CircularArc((255, 255, 255), [width/2, height/2], 100, tile_num*(2*pi/7), (tile_num*(2*pi/7))+2*pi/7, int(ring_width*0.5))

while True:

    fill_color = black

    for event in pygame.event.get():
        # quit if the quit button was pressed
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()

    x, y = pygame.mouse.get_pos()


    # Change color when the mouse touches
    if arc.contains(x, y):
        fill_color = (200, 0, 0)


    screen.fill(fill_color)
    arc.draw(screen)
    # screen.blit(debug, (0, 0))
    pygame.display.update()
</code></pre>

python pygame collision-detection