Answer the question
In order to leave comments, you need to log in
How to write a condition to generate points inside an ellipse in canvas?
How to write a condition so that the points are generated inside the ellipse and?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<canvas style="border: 1px solid grey" id="canvas"></canvas>
</body>
</html>
const WIDTH = 300;
const HEIGHT = 300;
const canvas = document.getElementById('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
const ctx = canvas.getContext('2d');
const n = 10;
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
ctx.beginPath();
ctx.ellipse(100, 100, 50, 90, Math.PI / 2, 0, 2 * Math.PI);
ctx.stroke();
for (let i = 0; i < n; i++){
let x = getRandom(50,150);
let y = getRandom(50,150);
ctx.rect(x, y, 2,2);
ctx.stroke();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question