Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Created April 18, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/ec497e3594e2874b7a4413baf3d3a4bf to your computer and use it in GitHub Desktop.
Save DaisukeNagata/ec497e3594e2874b7a4413baf3d3a4bf to your computer and use it in GitHub Desktop.
Mathematics
import UIKit
class Pointted {
var horizonal: CGPoint
var vertical: CGPoint
init(h: CGPoint, v: CGPoint) {
horizonal = h
vertical = v
}
}
func angle(a: CGPoint, b: CGPoint) -> Double {
var r = atan2(b.y - a.y, b.x - a.x)
if r < 0 {
r = r + CGFloat(2 * Double.pi)
}
return Double(r * 360 / CGFloat(2 * Double.pi))
}
func angleLength( p1: CGPoint, _ p2: Pointted) -> CGFloat {
return sqrt(pow(p1.x - p2.horizonal.x, 2) + pow(p1.y - p2.vertical.y, 2))
}
func distance(a: CGPoint, b: CGPoint) -> Double {
return sqrt(Double(pow(b.x - a.x, 2) + pow(b.y - a.y, 2)))
}
func coordinate(cg: Double)->(Double, Double, Double) {
let radian: Double = cg * Double.pi / 180
return(sin(radian), cos(radian) , tan(radian))
}
// 水平線からの角度を算出
print(angle(a: CGPoint(x: 50, y: 50), b: CGPoint(x: 100, y: 100)), "angle")
// 放物線の距離を算出
let p = Pointted(h: CGPoint(x: 50, y: 50), v: CGPoint(x: 100, y: 100))
print(angleLength(p1: CGPoint(x: 50, y: 50), p), "angleLength")
// 座標に対する距離を算出
print(distance(a: CGPoint(x: 50, y: 50), b: CGPoint(x: 100, y: 100)), "distance")
// sin 角度に対する 正弦に相当  cos 角度に対する余弦に相当  tan 角度に対する正接に相当
print(coordinate(cg: 100), "coordinate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment