Reputation: 940
I have a circle and a random point I set on it. This random point is currently really random but I want it to be on the center of a division on the grid in the background.
I made a snippet with the current random point:
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const size = 512
canvas.width = size
canvas.height = size
// Draw grid
ctx.beginPath()
ctx.strokeStyle = '#000'
const gridDivisions = 10
const gridSize = size / gridDivisions
for (let i = 0; i <= gridDivisions; i++) {
ctx.moveTo(0, i * gridSize)
ctx.lineTo(size, i * gridSize)
ctx.moveTo(i * gridSize, 0)
ctx.lineTo(i * gridSize, size)
}
ctx.stroke()
ctx.closePath()
// Draw circle
const radius = 180
ctx.beginPath()
ctx.strokeStyle = '#F00'
ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
// Draw random point
const angle = Math.random() * Math.PI * 2
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#000'
ctx.translate(Math.cos(angle) * radius + size / 2, Math.sin(angle) * radius + size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
// Draw center
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#00F'
ctx.translate(size / 2, size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
<canvas id="canvas"></canvas>
and I want the black cross to be at the center of a random grid division, like this (in any division that collides with the circle):
Upvotes: 2
Views: 63
Reputation: 18378
You just need to scale down coordinates of the point to the cell size, then take their integer values, scale them back and add a half of the cell size.
Easier to show, see the snippet below:
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const size = 512
canvas.width = size
canvas.height = size
// Draw grid
ctx.beginPath()
ctx.strokeStyle = '#000'
const gridDivisions = 10
const gridSize = size / gridDivisions
for (let i = 0; i <= gridDivisions; i++) {
ctx.moveTo(0, i * gridSize)
ctx.lineTo(size, i * gridSize)
ctx.moveTo(i * gridSize, 0)
ctx.lineTo(i * gridSize, size)
}
ctx.stroke()
ctx.closePath()
// Draw circle
const radius = 180
ctx.beginPath()
ctx.strokeStyle = '#F00'
ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
// Draw random point
const angle = Math.random() * Math.PI * 2
let tx = Math.cos(angle) * radius + size / 2;
let ty = Math.sin(angle) * radius + size / 2;
[tx, ty] = [tx, ty].map(c => (c / gridSize | 0) * gridSize + gridSize / 2);
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#000'
ctx.translate(tx, ty)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
// Draw center
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#00F'
ctx.translate(size / 2, size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
<canvas id="canvas"></canvas>
Upvotes: 2