Skip to content

Commit fd21451

Browse files
committed
started work on circleOutline code
1 parent b8a7492 commit fd21451

11 files changed

+636
-61
lines changed

box-circleOutline.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var circleOutlineBox = require('./circleOutline-box')
2+
3+
/**
4+
* circleOutline-box (axis-aligned) collision
5+
* @param {number} xc center of circle
6+
* @param {number} yc center of circle
7+
* @param {radius} rc radius of circle
8+
* @param {number} x top-left corner of box
9+
* @param {number} y top-left corner of box
10+
* @param {number} width of box
11+
* @param {number} height of box
12+
* @param {number} thickness of circle outline
13+
*/
14+
module.exports = function boxCircleOutline(x, y, width, height, xc, yc, rc, thickness)
15+
{
16+
return circleOutlineBox(xc, yc, rc, x, y, width, height, thickness)
17+
}

circleOutline-box.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var circlePoint = require('./circle-point')
2+
var boxCircle = require('./box-circle')
3+
4+
/**
5+
* circleOutline-box (axis-aligned) collision
6+
* @param {number} xc center of circle
7+
* @param {number} yc center of circle
8+
* @param {radius} rc radius of circle
9+
* @param {number} x top-left corner of box
10+
* @param {number} y top-left corner of box
11+
* @param {number} width of box
12+
* @param {number} height of box
13+
* @param {number} thickness of circle outline
14+
*/
15+
module.exports = function circleOutlineBox(xc, yc, rc, x, y, width, height, thickness)
16+
{
17+
thickness = thickness || 1
18+
var count = 0
19+
count += circlePoint(xc, yc, rc, x, y) ? 1 : 0
20+
count += circlePoint(xc, yc, rc, x + width, y) ? 1 : 0
21+
count += circlePoint(xc, yc, rc, x, y + height) ? 1 : 0
22+
count += circlePoint(xc, yc, rc, x + width, y + height) ? 1 : 0
23+
24+
// if no corners are inside the circle, then intersects only if box encloses circle-outline
25+
if (count === 0)
26+
{
27+
return boxCircle(x, y, width, height, xc, yc, rc)
28+
}
29+
30+
// if one corner is inside and one corner is outside then box intersects circle-outline
31+
if (count >= 1 && count <= 3)
32+
{
33+
return true
34+
}
35+
36+
// last check is if box is inside circle, need to check that a corner is not inside the inner circle
37+
if (count === 4)
38+
{
39+
return !circlePoint(xc, yc, rc - thickness, x, y) ||
40+
!circlePoint(xc, yc, rc - thickness, x + width, y) ||
41+
!circlePoint(xc, yc, rc - thickness, x, y + height) ||
42+
!circlePoint(xc, yc, rc - thickness, x + width, y + height)
43+
}
44+
}

circleOutline-line.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var lineCircle = require('./line-circle')
2+
var circlePoint = require('./circle-point')
3+
4+
/**
5+
* circleOutline-line collision
6+
* @param {number} xc center of circle
7+
* @param {number} yc center of circle
8+
* @param {radius} rc radius of circle
9+
* @param {number} x1 of point 1 of line
10+
* @param {number} y1 of point 1 of line
11+
* @param {number} x2 of point 2 of line
12+
* @param {number} y2 of point 2 of line
13+
* @param {number} thickness of circle outline
14+
*/
15+
module.exports = function circleOutlineLine(xc, yc, rc, x1, y1, x2, y2, thickness)
16+
{
17+
thickness = thickness || 1
18+
return lineCircle(x1, y1, x2, y2, xc, yc, rc) && !(circlePoint(xc, yc, rc - thickness, x1, y1) && circlePoint(xc, yc, rc - thickness, x2, y2))
19+
}

circleOutline-point.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var circlePoint = require('./circle-point')
2+
3+
/**
4+
* circleOutline-point collision
5+
* @param {number} xc center of circle
6+
* @param {number} yc center of circle
7+
* @param {radius} rc radius of circle
8+
* @param {number} x of point
9+
* @param {number} y of point
10+
* @param {number} thickness of circle outline
11+
*/
12+
module.exports = function circleOutlinePoint(xc, yc, rc, x, y, thickness)
13+
{
14+
thickness = thickness || 1
15+
return circlePoint(xc, yc, rc, x, y) && !circlePoint(xc, yc, rc - thickness, x, y)
16+
}

docs/code.js

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,80 @@ function ellipseEllipse(x, y)
502502
text('ellipseEllipse()', x, y)
503503
}
504504

505+
function circleOutlineBox(x, y)
506+
{
507+
function change()
508+
{
509+
const r = Random.range(shape * 0.1, shape)
510+
const w = Random.range(shape * 0.1, shape)
511+
const h = Random.range(shape * 0.1, shape)
512+
ease.to(c, { x: Random.range(x + r, x + square - r), y: Random.range(y + r, y + square - r), r }, TIME)
513+
const to = ease.to(rectangle, { x: Random.range(x + w, x + square - w), y: Random.range(y + h, y + square - h), w, h }, TIME)
514+
to.on('each', () =>
515+
{
516+
const tint = (REVERSE ?
517+
Intersects.circleOutlineBox(c.x, c.y, c.r, r.x, r.y, r.w, r.h) :
518+
Intersects.boxCircleOutline(rectangle.x, rectangle.y, rectangle.w, rectangle.h, c.x, c.y, c.r)) ? 0xff0000 : 0x00ff00
519+
g.clear()
520+
.lineStyle(1, tint).drawCircle(c.x, c.y, c.r).lineStyle(0)
521+
.beginFill(tint).drawRect(rectangle.x, rectangle.y, rectangle.w, rectangle.h).endFill()
522+
})
523+
to.on('done', change)
524+
}
525+
const g = renderer.stage.addChild(new PIXI.Graphics())
526+
const c = { x: x + square / 2 - small / 2, y: y + square / 2 - small / 2, r: small }
527+
const w = Random.range(shape * 0.1, shape)
528+
const h = Random.range(shape * 0.1, shape)
529+
const rectangle = { x: Random.range(x + w, x + square - w), y: Random.range(y + h, y + square - h), w, h }
530+
531+
change()
532+
text(REVERSE ? 'circleOutlineBox()' : 'boxCircleOutline()', x, y)
533+
}
534+
535+
function circleOutlineLine(x, y)
536+
{
537+
function change()
538+
{
539+
const to = ease.to(line, { x1: Random.range(x, x + square), y1: Random.range(y, y + square), x2: Random.range(x, x + square), y2: Random.range(y, y + square) }, TIME)
540+
to.on('each', () =>
541+
{
542+
const tint = (REVERSE ? Intersects.lineCircleOutline(line.x1, line.y1, line.x2, line.y2, circle.x, circle.y, circle.radius) :
543+
Intersects.circleOutlineLine(circle.x, circle.y, circle.radius, line.x1, line.y1, line.x2, line.y2)) ? 0xff0000 : 0x00ff00
544+
g.clear()
545+
.lineStyle(dot, tint).moveTo(line.x1, line.y1).lineTo(line.x2, line.y2)
546+
.lineStyle(1, tint).drawCircle(circle.x, circle.y, circle.radius).lineStyle(0)
547+
548+
})
549+
to.on('done', change)
550+
}
551+
const g = renderer.stage.addChild(new PIXI.Graphics())
552+
const line = { x1: Random.range(x, x + square), y1: Random.range(y, y + square), x2: Random.range(x, x + square), y2: Random.range(y, y + square) }
553+
const circle = { x: x + square / 2, y: y + square / 2, radius: radius * 1.4 }
554+
change()
555+
text(REVERSE ? 'lineCircleOutline()' : 'circleOutlineLine()', x, y)
556+
}
557+
558+
function circlePointOutline(x, y)
559+
{
560+
function change()
561+
{
562+
const angle = Random.angle()
563+
const distance = Random.get(square * 0.4)
564+
const xTo = x + square / 2 + Math.cos(angle) * distance
565+
const yTo = y + square / 2 + Math.sin(angle) * distance
566+
const to = ease.target(point, { x: xTo, y: yTo }, speed)
567+
to.on('each', () =>
568+
circle.tint = (REVERSE ? Intersects.pointCircleOutline(point.x, point.y, circle.x, circle.y, radius, thickness) :
569+
Intersects.circleOutlinePoint(circle.x, circle.y, radius, point.x, point.y, thickness)) ? 0xff0000 : 0x00ff00)
570+
to.on('done', change)
571+
}
572+
const thickness = 5
573+
const circle = drawCircle(x + square / 2, y + square / 2, radius, 0xffffff, thickness)
574+
const point = drawCircle(x, y, dot, 0)
575+
change()
576+
text(REVERSE ? 'pointCircleOutline()' : 'circlePointOutline()', x, y)
577+
}
578+
505579
// test for points on edge of polygon
506580
// see https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html (point on edge)
507581
function polygonPointSpecial(x, y)
@@ -584,18 +658,19 @@ function tests()
584658
next()
585659
ellipseEllipse(x, y)
586660
next()
661+
circleOutlineBox(x, y)
662+
next()
663+
circleOutlineLine(x, y)
664+
next()
665+
circlePointOutline(x, y)
666+
next()
587667
polygonPointSpecial(x, y)
588668
}
589669

590670
// sets up renderer, pixi-ease
591671
function setup()
592672
{
593-
renderer = new PIXI.Application({ transparent: true, resolution: window.devicePixelRatio })
594-
renderer.view.style.pointerEvents = 'none'
595-
renderer.view.style.position = 'fixed'
596-
renderer.view.style.width = '100vw'
597-
renderer.view.style.height = '100vh'
598-
renderer.view.style.left = renderer.view.style.top = 0
673+
renderer = new PIXI.Application({ transparent: true, resolution: window.devicePixelRatio, view: document.querySelector('.view') })
599674
document.body.appendChild(renderer.view)
600675
ease = new Ease.list()
601676
}
@@ -637,14 +712,21 @@ function resize()
637712

638713
renderer.stage.removeChildren()
639714
tests()
640-
renderer.renderer.resize(x, y)
715+
renderer.renderer.resize(x, y + square)
641716
}
642717

643-
function drawCircle(x, y, r, color)
718+
function drawCircle(x, y, r, color, outline)
644719
{
645720
color = typeof color === 'undefined' ? 0xffffff : color
646721
const circle = renderer.stage.addChild(new PIXI.Graphics())
647-
circle.beginFill(color).drawCircle(0, 0, r).endFill()
722+
if (outline)
723+
{
724+
circle.lineStyle(outline === true ? 1 : outline, color).drawCircle(0, 0, r).lineStyle(0)
725+
}
726+
else
727+
{
728+
circle.beginFill(color).drawCircle(0, 0, r).endFill()
729+
}
648730
circle.position.set(x, y)
649731
return circle
650732
}

docs/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
.hljs-comment {
1818
color: blue;
1919
}
20+
.view {
21+
width: 100vw;
22+
pointer-events: none;
23+
}
2024
</style>
2125
</head>
2226
<body>
2327
<script src="index.js"></script>
2428
<div class="title">Example for <a href="https://github.com/davidfig/intersects/">github.com/davidfig/intersects/</a> (<a href="https://www.npmjs.com/package/intersects">npmjs: intersects</a>)</a></div>
29+
<canvas class="view"></canvas>
2530
</body>

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module.exports = {
66
circlePolygon: require('./circle-polygon'),
77
circleEllipse: require('./circle-ellipse'),
88

9+
circleOutlineBox: require('./circleOutline-box'),
10+
circleOutlineLine: require('./circleOutline-line'),
11+
circleOutlinePoint: require('./circleOutline-point'),
12+
913
polygonPoint: require('./polygon-point'),
1014
polygonLine: require('./polygon-line'),
1115
polygonPolygon: require('./polygon-polygon'),
@@ -19,19 +23,22 @@ module.exports = {
1923
boxPolygon: require('./box-polygon'),
2024
boxCircle: require('./box-circle'),
2125
boxEllipse: require('./box-ellipse'),
26+
boxCircleOutline: require('./box-circleOutline'),
2227

2328
pointBox: require('./point-box'),
2429
pointPolygon: require('./point-polygon'),
2530
pointCircle: require('./point-circle'),
2631
pointLine: require('./point-line'),
2732
pointEllipse: require('./point-ellipse'),
33+
pointCircleOutline: require('./point-circleOutline'),
2834

2935
lineLine: require('./line-line'),
3036
lineBox: require('./line-box'),
3137
linePolygon: require('./line-polygon'),
3238
lineCircle: require('./line-circle'),
3339
linePoint: require('./line-point'),
3440
lineEllipse: require('./line-ellipse'),
41+
lineCircleOutline: require('./line-circleOutline'),
3542

3643
ellipsePoint: require('./ellipse-point'),
3744
ellipseLine: require('./ellipse-line'),

line-circleOutline.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var circleOutlineLine = require('./circleOutline-line')
2+
3+
/**
4+
* line-circleOutline collision
5+
* @param {number} x1 of point 1 of line
6+
* @param {number} y1 of point 1 of line
7+
* @param {number} x2 of point 2 of line
8+
* @param {number} y2 of point 2 of line
9+
* @param {number} xc center of circle
10+
* @param {number} yc center of circle
11+
* @param {radius} rc radius of circle
12+
* @param {number} thickness of circle outline
13+
*/
14+
module.exports = function lineCircleOutline(x1, y1, x2, y2, xc, yc, rc, thickness)
15+
{
16+
return circleOutlineLine(xc, yc, rc, x1, y1, x2, y2, thickness)
17+
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "a simple collection of 2d collision/intersects functions, supporting points, circles, lines, axis-aligned boxes, and polygons (convex)",
55
"main": "index.js",
66
"scripts": {
7-
"test": "budo docs/code.js:index.js --dir docs",
7+
"test": "budo docs/code.js:index.js --dir docs --live",
88
"build": "browserify docs/code.js -o docs/index.js",
99
"prepublishOnly": "yarn run pkg && yarn run build",
1010
"pkg-browserify": "browserify umd.js -o umd/intersects.js",
@@ -23,9 +23,9 @@
2323
"homepage": "https://github.com/davidfig/intersects#readme",
2424
"devDependencies": {
2525
"calc-fontsize": "^1.1.0",
26-
"highlight.js": "^9.12.0",
27-
"pixi-ease": "^1.1.2",
28-
"pixi.js": "^4.6.2",
29-
"yy-random": "^1.7.3"
26+
"highlight.js": "^9.15.6",
27+
"pixi-ease": "^1.3.0",
28+
"pixi.js": "^5.0.2",
29+
"yy-random": "^1.8.0"
3030
}
3131
}

point-circleOutline.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var circleOutlinePoint = require('./circleOutline-point')
2+
3+
/**
4+
* point-circleOutline collision
5+
* @param {number} x of point
6+
* @param {number} y of point
7+
* @param {number} xc center of circle
8+
* @param {number} yc center of circle
9+
* @param {radius} rc radius of circle
10+
* @param {number} thickness of circle outline
11+
*/
12+
module.exports = function pointCircleOutline(x, y, xc, yc, rc, thickness)
13+
{
14+
return circleOutlinePoint(x, y, xc, yc, rc, thickness)
15+
}

0 commit comments

Comments
 (0)