随机生成多个不重叠圆圈 纯javascript

随机生成多个不重叠圆圈 纯javascript

<template>
    <div
        class="wrapper"
        :style="{ width: width + 'px', height: height + 'px' }"
    >
        <div
            class="circle"
            v-for="item in pos"
            :style="{
                left: item.x + 'px',
                top: item.y + 'px',
                width: item.width + 'px',
                height: item.height + 'px',
            }"
        ></div>
    </div>
</template>

<script setup name="circles" lang="ts">
import { onMounted } from "vue";
// 总的宽高
let width = 1920;
let height = 1080;

// 每个模块的最大,最小宽度(因为画圆,所以宽高一致,割出来的是正方形)
let minr = 40;
let minR = 120;
let maxR = 150;

let leave = 0;
onMounted(() => {
    getColWS();
});
let colWS = [];
let pos = ref([]);
function getColWS() {
    let len = 0;
    while (len < width - maxR) {
        let w = Math.floor(Math.random() * (maxR - minR) + minR);
        colWS.push(w);
        len += w + Math.random() * 5;
        leave = width - len;
    }
    colWS.push(leave);
    getPosition();
}

function getPosition() {
    let x = 0;
    colWS.forEach((col) => {
        let y = 0;

        while (y <= height - 10) {
            let h = Math.floor(Math.random() * (col - minr) + minr);
            if (y + h > height) {
                h = height - y;
            }
            if (h >= minr) {
                let rate = Math.floor(col / h);
                for (let i = 0; i < rate; i++) {
                    let p = getCirclePosition({
                        x: x + (i * col) / rate,
                        y: y,
                        width: h,
                        height: h,
                    });
                    pos.value.push({
                        x: p.x,
                        y: p.y,
                        width: p.r,
                        height: p.r,
                    });
                }
            }
            y += h + Math.random() * 5;
        }
        x += col;
    });
}
function getCirclePosition(item) {
    let r = Math.random() * (item.height - minr) + minr;
    let x = item.x + Math.random() * (item.width - r);
    let y = item.y + Math.random() * (item.width - r);
    return { x, y, r };
}
</script>

<style lang="scss" scoped>
.wrapper {
    width: 960px;
    height: 540px;
    border: 1px solid red;
    position: relative;
    .canvas {
        width: 100%;
        height: 100%;
    }
    .circle {
        position: absolute;
        border: 1px solid red;
        border-radius: 50%;
    }
}
</style>

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现随机生成多个圆并且不重叠,可以按照以下步骤进行: 1. 定义一个存储圆的数组,用来检查新生成的圆是否与已有圆重叠。 2. 定义一个函数来生成圆,函数需要传入画布上下文和画布的宽度和高度。在函数内部,生成一个随机的圆心坐标和半径。 3. 在生成圆之前,先检查新生成的圆是否与已有圆重叠。可以通过遍历数组中的圆,检查它们之间的距离是否小于两圆半径之和来实现。 4. 如果新生成的圆与已有圆重叠,则重新生成圆;否则,将新的圆添加到数组中,并在画布上绘制出来。 下面是一个简单的例子代码,可以生成随机位置和大小的圆,并且保证不重叠: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>随机圆不重叠</title> </head> <body> <canvas id="myCanvas" width="600" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var circles = []; function generateCircle(width, height) { var radius = Math.floor(Math.random() * 50) + 10; var x = radius + Math.floor(Math.random() * (width - 2 * radius)); var y = radius + Math.floor(Math.random() * (height - 2 * radius)); for (var i = 0; i < circles.length; i++) { var dx = circles[i].x - x; var dy = circles[i].y - y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < circles[i].radius + radius) { return generateCircle(width, height); } } return {x: x, y: y, radius: radius}; } for (var i = 0; i < 10; i++) { var circle = generateCircle(canvas.width, canvas.height); circles.push(circle); ctx.beginPath(); ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI); ctx.stroke(); ctx.closePath(); } </script> </body> </html> ``` 这段代码会在画布上生成10个随机位置和大小的圆,保证它们不重叠。你可以将画布的宽高和圆的个数调整为自己需要的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺平,睡觉

苦逼三线前端,给点支持吧。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值