作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个连接两个房间和走廊的功能。看起来效率很低,但我找不到更好的方法。它是我用于生成地下城的 BSP 算法的一部分。您可以找到完整的算法 here .
public func createHall(left:Room, right:Room) {
// Connects 2 rooms together with hallways
// Reset hallways function to make sure its empty. hallways = [Room]()
hallways = []
// get width and height of first room
let point1 = CGPoint(x: Int.random(in: (left.x1 + 1)..<(left.x2 - 1)),
y: Int.random(in: (left.y1 + 1)..<(left.y2 - 1)))
// get width and height of second room
let point2 = CGPoint(x: Int.random(in: (right.x1 + 1)..<(right.x2 - 1)),
y: Int.random(in: (right.y1 + 1)..<(right.y2 - 1)))
let w = point2.x - point1.x
let h = point2.y - point1.y
if w < 0 {
if h < 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
}
} else if h > 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
}
} else if w > 0 {
if h < 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
}
} else if h > 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
}
} else {
if h < 0 {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else if h > 0 {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
}
}
Room 是我编写的自定义类,用于计算矩形及其中心:
class Room {
var x1:Int
var x2:Int
var y1:Int
var y2:Int
var center:CGPoint
init(X: Int, Y: Int, W: Int, H: Int) {
x1 = X
x2 = X + W
y1 = Y
y2 = Y + H
center = CGPoint(x: (x1 + x2) / 2, y: (y1 + y2) / 2)
}
}
我最成功的尝试:
func hCorridor(x1: Int, x2: Int, y: Int) {
for x in min(x1,x2)...max(x1,x2) {
hallways.append(Room(X: y, Y: y, W: 1, H: Int(abs(h))))
}
}
func vCorridor(y1: Int, y2: Int, x: Int) {
for y in min(y1,y2)...max(y1,y2) {
hallways.append(Room(X: y, Y: y, W: Int(abs(w), H: 1))
}
}
// Randomly choose to start with horizontal or vertical corridors
if Double.random(in: 0..<1.0) > 0.5 {
hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point1.y))
vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point2.x))
} else {
vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point1.x))
hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point2.y))
}
createHall()
函数中的所有这些 if 语句真的有必要吗?如果没有,写它们的更好方法是什么?我所有的尝试都不像 if 语句那样有效。我的尝试给了我死胡同和无法进入的房间。
最佳答案
如果我正确理解你的问题,你
point1
, point2
(resp. right)房间,和point1
到point2
的两条“走廊”连接房间,先水平然后垂直,反之亦然。除了随机选择之外,如果您使用,则不需要 if 语句min
和 abs
计算走廊坐标。类似的东西(内联更多解释):
if Bool.random() {
// Horizontally first, then vertically:
// From point1 to (point2.x, point1.y):
hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point1.y),
W: Int(abs(point1.x - point2.x)), H: 1))
// From (point2.x, point1.y) to point2:
hallways.append(Room(X: Int(point2.x), Y: Int(min(point1.y, point2.y)),
W: 1, H: Int(abs(point1.y - point2.y))))
} else {
// Vertically first, then Horizontally:
// From point1 to (point1.x, point2.y):
hallways.append(Room(X: Int(point1.x), Y: Int(min(point1.y, point2.y)),
W: 1, H: Int(abs(point1.y - point2.y))))
// From (point1.x, point2.y) to point2:
hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point2.y),
W: Int(abs(point1.x - point2.x)), H: 1))
}
关于ios - 所有这些 if 语句真的有必要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678708/
我正在使用 this solution在二进制矩阵中找到与图像边界对齐的矩形。假设现在我想找到一个不与图像边框对齐的矩形,并且我不知道它的方向;找到它的最快方法是什么? 为了示例,让我们寻找一个仅包含
else: 行在这个 Python 程序中是否正确/必要? from random import randrange for n in range(10): r = randrange(0,1
在 TDPL 7.1.5.1 中讨论了将 Widget w2 分配给 w1 并且作者指出“将 w2 逐个字段分配给 w1 会将 w2.array 分配给 w1.array——一个简单的数组边界分配,而
我是一名优秀的程序员,十分优秀!