두 CG 포인트 사이의 거리를 찾는 방법은 무엇입니까?
UIScrollView에서 두 손가락으로 멀티 터치를하면 두 개의 CG 포인트를 얻습니다. 나는 그들 사이의 거리를 찾고 싶다. 그런 다음 다시 핀치 (내부 또는 외부)를 수행하면 다시 두 점을 얻습니다. 그런 다음이 두 점 사이의 거리를 다시 찾은 후 끼어들 었는지 아니면 끼 었는지 결정하고 싶습니다. 내가 끼어들었다면 확실히 새로운 거리는 더 줄어들 것이고 그 반대의 경우도 마찬가지입니다.
그러나 비교를 위해 두 지점 사이의 거리에 대한 정확한 측정을 찾는 방법을 모르십니까? 누구든지 이것에 대해 생각하고 있습니까?
p1
과 사이의 거리 p2
:
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt(xDist * xDist + yDist * yDist);
함수를 넣으십시오.
func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(sqrt(xDist * xDist + yDist * yDist))
}
배경 : 피타고라스 정리
점 사이의 거리가 증가하거나 감소하는 경우에만 계산해야하는 경우 sqrt ()를 생략하면 조금 더 빨라집니다.
hypot()
또는 hypotf()
함수를 사용하여 빗변을 계산할 수 있습니다 . 두 점이 주어 p1
지고 p2
:
CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);
그리고 그게 다야.
신속한 사용자를 위해
extension CGPoint {
func distance(to point: CGPoint) -> CGFloat {
return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
}
}
-(float)distanceFrom:(CGPoint)point1 to:(CGPoint)point2
{
CGFloat xDist = (point2.x - point1.x);
CGFloat yDist = (point2.y - point1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}
cocos2d를 사용하는 경우
float distance = ccpDistance(point1, point2);
Swift 4를 사용하면 두 CGPoint
인스턴스 간의 거리를 확인하기 위해 다음 5 가지 플레이 그라운드 코드 중 하나를 선택할 수 있습니다 .
1. Darwin sqrt(_:)
기능 사용
import CoreGraphics
func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
let xDistance = lhs.x - rhs.x
let yDistance = lhs.y - rhs.y
return sqrt(xDistance * xDistance + yDistance * yDistance)
}
let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)
distance(from: point1, to: point2) // 701.141925718324
2. 사용 CGFloat
squareRoot()
방법
import CoreGraphics
func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
let xDistance = lhs.x - rhs.x
let yDistance = lhs.y - rhs.y
return (xDistance * xDistance + yDistance * yDistance).squareRoot()
}
let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)
distance(from: point1, to: point2) // 701.141925718324
3. 사용 CGFloat
squareRoot()
방법 및 Core Graphics pow(_:_:)
기능
import CoreGraphics
func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return (pow(lhs.x - rhs.x, 2) + pow(lhs.y - rhs.y, 2)).squareRoot()
}
let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)
distance(from: point1, to: point2) // 701.141925718324
4. Core Graphics hypot(_:_:)
기능 사용
import CoreGraphics
func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return hypot(lhs.x - rhs.x, lhs.y - rhs.y)
}
let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)
distance(from: point1, to: point2) // 701.141925718324
5. Core Graphics hypot(_:_:)
기능 및 CGFloat
distance(to:)
방법 사용
import CoreGraphics
func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return hypot(lhs.x.distance(to: rhs.x), lhs.y.distance(to: rhs.y))
}
let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)
distance(from: point1, to: point2) // 701.141925718324
나는 이것을 썼고 많이 사용합니다.
- (float) distanceBetween : (CGPoint) p1 and: (CGPoint) p2
{
return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}
다음과 같이 전화하십시오.
float distanceMoved = [self distanceBetween touchStart and: touchEnd];
I normally use cocos2d, but I still use my own function for some things because when I was learning I wrote a bunch of my own functions for simple stuff rather than searching for the "official" higher order functions, and additionally I'm not a big fan of functions(vars, vars), I prefer [self functions vars and: vars]
#define rw_pointOffset(point1, point2) CGPointMake(point2.x - point1.x, point2.y - point1.y)
#define rw_pointDistance(point1, point2) sqrtf( powf(point2.x - point1.x, 2.0f) + powf(point2.y - point1.y, 2.0f))
And that´s how you use it:
CGPoint offset = rw_pointOffset(view1.center, view2.center);
float distance = rw_pointDistance(view1.center, view2.center);
If you want to find the absolute distance value between two points then you can use (for Cocos2d):
float distance = abs(ccpDistance(point1, point2));
참고URL : https://stackoverflow.com/questions/1906511/how-to-find-the-distance-between-two-cg-points
'developer tip' 카테고리의 다른 글
PHP로 임의의 16 진수 색상 코드 생성 (0) | 2020.11.09 |
---|---|
Android Studio 설치 중 유효한 JVM 설치 오류를 가리 키지 않음 (0) | 2020.11.09 |
해결 실패 : Gradle을 사용하는 IntelliJ Idea의 com.google.android.gms : play-services (0) | 2020.11.09 |
UIView의 배경 이미지를 채우는 방법 (0) | 2020.11.09 |
Eclipse 용 PHP Mess Detector (0) | 2020.11.09 |