作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设您有一个矩形,width=10, height=20
并且想要获取周边每个像素的坐标。您本可以使用 4 个 for 循环完成它,但是没有更好、更简洁的方法吗?
for(x=0; x<width; x++){
doSomethingWith(x,0)
}
for(y=0; y<height; y++){
doSomethingWith(0,y)
}
for(x=1; x<width; x++){
doSomethingWith(x,height)
}
for(y=1; y<height; y++){
doSomethingWith(width,y)
}
我将使用 javascript,但欢迎提供伪代码或其他语言的帮助。
最佳答案
你可以只用两个 for
循环来完成:
for (x = 0; x < width; x++) {
doSomethingWith(x, 0)
doSomethingWith(x, height - 1)
}
for (y = 0; y < height; y++) {
doSomethingWith(0, y)
doSomethingWith(width - 1, y)
}
例子:
var x, y, width = 10, height = 20;
for (x = 0; x < width; x++) {
doSomethingWith(x, 0)
doSomethingWith(x, height - 1)
}
for (y = 0; y < height; y++) {
doSomethingWith(0, y)
doSomethingWith(width - 1, y)
}
function doSomethingWith(x, y) {
var b = document.createElement("div");
b.className = "block";
b.style.left = (x * 10) + "px";
b.style.top = (y * 10) + "px";
document.body.appendChild(b)
}
.block {
box-sizing: border-box;
position: absolute;
width: 10px;
height: 10px;
background-color: red;
border: 1px solid black;
}
关于javascript - 迭代矩形的周长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42395696/
好吧,这不起作用!有什么问题呢。有人可以向我指出吗?我已经盯着它看了好几个小时了。我之前寻求过帮助,他提出了结构,但我并没有真正理解它,所以它不起作用。 没有得到正确的计算。我认为读取文件时可能出现问
我需要显示一个圆周。为了做到这一点,我认为我可以为很多 x 计算 y 的两个值,所以我做了: import sympy as sy from sympy.abc import x,y f = x**2
3个不同的类 1个用于处理Circle实例,1个用于Square实例,第3个用于比较之间他们(main) 。在 main 函数中,我找到了 circle (在 c1..c4 之间)和 square (
我是一名优秀的程序员,十分优秀!