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
网友评论