import Foundation
let restaurantLocation = (2, 4)
let restaurantRange = 2.5
let otherRestaurantLocation = (7, 8)
let otherRestaurantRange = 1.5
// Pythagorean Theorem 📐 🎓
func distance(from source: (x: Int, y: Int), to target: (x: Int, y: Int)) -> Double {
let distanceX = Double(source.x - target.x)
let distanceY = Double(source.y - target.y)
return (distanceX * distanceX + distanceY * distanceY).squareRoot()
}
func isInDeliveryRange(location: (x: Int, y: Int)) -> Bool {
let deliveryDistance = distance(from: location, to: restaurantLocation)
let secondDeliveryDistance = distance(from: location, to: otherRestaurantLocation)
return deliveryDistance < restaurantRange || secondDeliveryDistance < otherRestaurantRange
}
struct Location {
let x: Int
let y: Int
}
let storeLocation = Location(x: 2, y: 4)
struct DeliveryArea: CustomStringConvertible {
let center: Location
var radius: Double
var description: String {
return """
Area with center: x: \(center.x) - y: \(center.y),
radius: \(radius)
"""
}
func contains(_ location: Location) -> Bool {
let distanceFromCenter = distance(from: (center.x, center.y), to: (location.x, location.y))
return distanceFromCenter < radius
}
}
var storeArea = DeliveryArea(center: storeLocation, radius: 4)
print(storeArea.radius) // 4.0
print(storeArea.center.x) // 2
storeArea.radius = 250
var fixedArea = DeliveryArea(center: storeLocation, radius: 4)
// Error: change let to var above to make it mutable.
fixedArea.radius = 250
let areas = [
DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5),
DeliveryArea(center: Location(x: 9, y: 7), radius: 4.5)
]
func isInDeliveryRange(_ location: Location) -> Bool {
for area in areas {
let distanceToStore = distance(from: (area.center.x, area.center.y), to: (location.x, location.y))
if distanceToStore < area.radius {
return true
}
}
return false
}
let customerLocation1 = Location(x: 8, y: 1)
let customerLocation2 = Location(x: 5, y: 5)
print(isInDeliveryRange(customerLocation1)) // false
print(isInDeliveryRange(customerLocation2)) // true
let area = DeliveryArea(center: Location(x: 5, y: 5), radius: 4.5)
let customerLocation = Location(x: 2, y: 2)
area.contains(customerLocation) // true
var a = 5
var b = a
print(a) // 5
print(b) // 5
a = 10
print(a) // 10
print(b) // 5
var area1 = DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5)
var area2 = area1
print(area1.radius) // 2.5
print(area2.radius) // 2.5
area1.radius = 4
print(area1.radius) // 4.0
print(area2.radius) // 2.5
print(area1) // Area with center: x: 2 - y: 4, radius: 4.0
print(area2) // Area with center: x: 2 - y: 4, radius: 2.5
Structures
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- An instance of a class is traditionally know as an object...
- 本教程为python3文档解读 本教程面向完全型小白,只要你会在电脑上打字,那你就可以看懂。 参考视频观看,味道更...
- Structures, or structs, are one of the named types in Swi...
- 对于宝妈来说。时间的灵活性是非常重要的! 很多宝妈本身是有很好的工作的。但是因为不想错过娃珍贵的每一个成长阶段而辞...