# Mouse inside custome shape

**URL:** <https://discourse.processing.org/t/mouse-inside-custome-shape/22110>\
**Category:** Beginners\
**Created:** [June 23, 2020, 5:51pm UTC](https://discourse.processing.org/t/mouse-inside-custome-shape/22110 "2020-06-23T17:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [June 23, 2020, 5:51pm UTC](https://discourse.processing.org/t/mouse-inside-custome-shape/22110/1 "2020-06-23T17:51:18Z")

</div>

Hi, i’m trying to get into programming again and a stubbled upon a little problem. I was trying to create a hexagon but I can’t figure out a smart way to detect when my mouse is inside of the shape. Any good ideas?

> void setup() {  
> fullScreen();  
> }  
> void draw() {  
> background(102);  
> polygon(width_0.5, height_0.5, 15, 6); //6-kant  
> }  
> void polygon(float x, float y, float radius, int npoints) {  
> float angle = TWO\_PI / npoints;  
> beginShape();  
> for (float a = 0; a \< TWO\_PI; a += angle) {  
> float sx = x + cos(a) \* radius;  
> float sy = y + sin(a) \* radius;  
> vertex(sx, sy);  
> }  
> endShape(CLOSE);  
> }

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [June 23, 2020, 5:57pm UTC](https://discourse.processing.org/t/mouse-inside-custome-shape/22110/2 "2020-06-23T17:57:14Z")

</div>

You’re in luck – I have a method for this (change the parameters/modify as you see fit):

```
/**
 * Determine if a point is in a polygon, given by a list of its vertices.
 * 
 * @param s PShape
 * @param point PVector point to check
 * @return boolean
 */
private static final boolean pointInPoly(PShape s, PVector point) {

	boolean within = false;
	int j = s.getVertexCount() - 1;

	for (int i = 0; i < s.getVertexCount(); i++) {
		final PVector v = s.getVertex(i);
		final PVector b = s.getVertex(j);
		if (((v.y > point.y) != (b.y > point.y)) && (point.x < (b.x - v.x) * (point.y - v.y) / (b.y - v.y) + v.x)) {
			within = !within;
		}
		j = i;
	}
	return within;
}
```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 23, 2020, 6:05pm UTC](https://discourse.processing.org/t/mouse-inside-custome-shape/22110/3 "2020-06-23T18:05:03Z")

</div>

> **[Collision Detection](http://jeffreythompson.org/collision-detection/poly-point.php)**
>
> An online book about collision detection using Processing.
