To achieve this functionality, you'll need to set up an HTML interface to accept width and length inputs for a rectangle, and then use JavaScript to draw the circles inside a rectangle on a 2D uniform grid. Additionally, you'll implement a shifted grid layout to maximize the number of circles that can fit within the same area.
Here's a basic implementation using HTML and JavaScript with the <canvas> element for rendering:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rectangle with Circles</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
canvas {
border: 1px solid black;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Draw Circles in a Rectangle</h1>
<label for="width">Width of Rectangle:</label>
<input type="number" id="width" value="300">
<label for="height">Height of Rectangle:</label>
<input type="number" id="height" value="200">
<button onclick="draw()">Draw Circles</button>
<canvas id="canvas" width="500" height="400"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const radius = 10;
function draw() {
const width = parseInt(document.getElementById('width').value, 10);
const height = parseInt(document.getElementById('height').value, 10);
canvas.width = width + 20; // Adding buffer for better visualization
canvas.height = height + 20;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw uniform grid
drawUniformGrid(width, height);
// Draw shifted grid
drawShiftedGrid(width, height);
}
function drawUniformGrid(width, height) {
ctx.strokeStyle = 'blue';
ctx.lineWidth = 1;
for (let y = radius; y <= height; y += 2 * radius) {
for (let x = radius; x <= width; x += 2 * radius) {
ctx.beginPath();
ctx.arc(x + 10, y + 10, radius, 0, Math.PI * 2);
ctx.stroke();
}
}
}
function drawShiftedGrid(width, height) {
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
const verticalSpacing = 2 * radius * Math.sin(Math.PI / 3);
for (let y = radius; y <= height; y += verticalSpacing) {
for (let x = radius; x <= width; x += 2 * radius) {
const offset = (Math.floor(y / verticalSpacing) % 2) * radius;
ctx.beginPath();
ctx.arc(x + offset + 10, y + 10, radius, 0, Math.PI * 2);
ctx.stroke();
}
}
}
</script>
</body>
</html>
Explanation:
-
HTML Elements: We set up inputs to specify the width and height of the rectangle and a button to trigger the drawing.
-
Canvas: The canvas element displays the rectangle and circles. Its size is adjusted based on the width and height inputs.
-
JavaScript Functions:
draw(): This function gets the values from the inputs, sets the canvas size, and calls the grid drawing functions.
drawUniformGrid(): This function determines the positions for drawing circles on a uniform grid by iterating over calculated positions based on the radius.
drawShiftedGrid(): This function draws circles using a hexagonal or offset grid, maximizing space usage by staggering rows.
-
Grid Coloring: The uniform and shifted grids are drawn with different colors for better differentiation (e.g., blue and red).
By interacting with this setup, users can visualize the circle arrangement within a rectangle using both uniform and shifted grids.