我试图遍历每个像素坐标,从 (0, 0) 开始,以便在它们不重叠的最近偏移处融合两个像素化形状。
到现在为止,我一直在使用同心正方形,这确实很容易做到,但最终可能会将嫁接图像放置得更远。然后我实现了 Bresenham 圆算法如下:
def generate_offsets(maxRadius : int):
"""Generate x and y coordinates in concentric circles around the origin
Uses Bresenham's Circle Drawing Algorithm
"""
for radius in range(maxRadius):
x = 0
y = radius
d = 3 - (2 * radius)
while x < y:
yield x, y
yield y, x
yield y, -x
yield x, -y
yield -x, -y
yield -y, -x
yield -y, x
yield -x, y
if d < 0:
d += (4 …Run Code Online (Sandbox Code Playgroud) 我知道这是一个奇怪的标题,但有很多帖子具有相似的标题和完全不同的问题。大多数人View在他们的视图中编写除代码之外的其他内容,而我没有这样做(据我所知)。
我正在尝试Picker与其他BinaryInteger类型兼容,因为它不适用于除 之外的任何类型Int,并且我在使预览正常工作时遇到了一些麻烦。这是代码:
import SwiftUI
struct CompatibilityPicker<Label, SelectionValue, Content> : View where Label : StringProtocol, SelectionValue : BinaryInteger, Content : View {
var content : () -> Content
var label : Label
@Binding private var _selection : SelectionValue
private var selection: Binding<Int> { Binding<Int>(
get: {
Int(_selection)
},
set: {
self._selection = SelectionValue($0)
})
}
init(_ label : Label, selection : SelectionValue, content : @escaping () -> Content) {
self.label = label …Run Code Online (Sandbox Code Playgroud) 作为课堂练习,我们应该根据年龄和性别计算夜总会人员的入场费。25 岁以下可享受 20% 折扣,女性/NB 可享受 50% 折扣,乘数叠加叠加。
虽然我的代码可以工作,但它会重复性别检查两次,这是一种糟糕的形式,可能会在更复杂的应用程序中导致问题。怎样才能避免重复呢?
(* OCaml Version *)
let entry_price age gender =
if age < 18
then (failwith "Must be over 18 to enter")
else let price = 12.0 in
if age <= 25
then let price = (price *. 0.8) in
if gender == 'f' || gender == 'x'
then (price *. 0.5)
else prix
else if gender == 'f' || gender == 'x'
then (price *. 0.5)
else price;;
Run Code Online (Sandbox Code Playgroud)
这是一个不会重复的 Python …