function drawRect (angle) {
let rotated = rotateRect(30, 21, angle)
// Rectangle is rotated around its center
let newHTML = ""
rotated.forEach(square=>{
newHTML += `<div class="square" style="left: ${square.x * 10}px; top: ${square.y * 10}px"></div>`
})
document.body.innerHTML = newHTML + "<div>Use the Left and Right Arrow Keys</div>"
}
let angle = 1
drawRect(angle)
document.addEventListener("keydown", event=>{
if (event.key == "ArrowRight") {
angle += 0.1
}
if (event.key == "ArrowLeft") {
angle -= 0.1
}
drawRect(angle)
})
function rotateRect(width, height, rotate /* in radians */) {
// Call the center of the rectangle [0,0]
rotate = Math.round(rotate * 1000) / 1000
while (rotate <= 0) rotate += Math.PI * 2
let fourCorners = []
fourCorners.push({x: Math.cos(Math.atan2(-height, -width) + rotate) * distance(width/2, height/2), y: Math.sin(Math.atan2(-height, -width) + rotate) * distance(width/2, height/2)})
fourCorners.push({x: Math.cos(Math.atan2(-height, width) + rotate) * distance(width/2, height/2), y: Math.sin(Math.atan2(-height, width) + rotate) * distance(width/2, height/2)})
fourCorners.push({x: Math.cos(Math.atan2(height, width) + rotate) * distance(width/2, height/2), y: Math.sin(Math.atan2(height, width) + rotate) * distance(width/2, height/2)})
fourCorners.push({x: Math.cos(Math.atan2(height, -width) + rotate) * distance(width/2, height/2), y: Math.sin(Math.atan2(height, -width) + rotate) * distance(width/2, height/2)})
for (let i = 1; i < rotate / (Math.PI / 2); i++) {
fourCorners.unshift(fourCorners.pop())
}
// let leftCorner = fourCorners.sort((a, b)=>a.x-b.x)[0]
// let rightCorner = fourCorners.sort((a, b)=>b.x-a.x)[0]
// let topCorner = fourCorners.sort((a, b)=>a.y-b.y)[0]
// let bottomCorner = fourCorners.sort((a, b)=>b.y-a.y)[0]
let topCorner = fourCorners[0]
let rightCorner = fourCorners[1]
let bottomCorner = fourCorners[2]
let leftCorner = fourCorners[3]
let yMin = topCorner.y
let xMax = rightCorner.x
let yMax = bottomCorner.y
let xMin = leftCorner.x
let t_l_angle = Math.atan2(topCorner.y - leftCorner.y, topCorner.x - leftCorner.x)
let t_r_angle = Math.atan2(rightCorner.y - topCorner.y, rightCorner.x - topCorner.x)
let b_r_angle = Math.atan2(bottomCorner.y - rightCorner.y, bottomCorner.x - rightCorner.x)
let b_l_angle = Math.atan2(leftCorner.y - bottomCorner.y, leftCorner.x - bottomCorner.x)
let coveredSquares = []
for (let x = Math.ceil(xMin); x < Math.floor(xMax); x++) {
for (let y = Math.ceil(yMin); y < Math.floor(yMax); y++) {
let local_t_l_angle = Math.atan2(y - leftCorner.y, x - leftCorner.x)
let local_t_r_angle = Math.atan2(y - topCorner.y, x+1 - topCorner.x)
let local_b_r_angle = Math.atan2(y+1 - rightCorner.y, x+1 - rightCorner.x)
let local_b_l_angle = Math.atan2(y+1 - bottomCorner.y, x - bottomCorner.x)
if (
(local_t_l_angle > t_l_angle) &&
(local_t_r_angle > t_r_angle) &&
(local_b_r_angle > b_r_angle || local_b_r_angle < b_r_angle - Math.PI) &&
(local_b_l_angle > b_l_angle)
) {
coveredSquares.push({x, y})
}
}
}
console.log(coveredSquares)
return coveredSquares
}
function distance(x, y) {
return Math.sqrt((x*x)+(y*y))
}
body {
}
.square {
position: absolute;
background-color: red;
width: 9px;
height: 9px;
z-index: 1000;
transform: translate(150px, 150px);
}