자바 스크립트 (ES6), 316 323 347
p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'
매트릭스 및 결정자를 처리하는 데 더 적합한 언어는 점수가 더 높아야합니다 (APL, J, CJAM, Jelly)
참고 : 원뿔의 일반적인 형태는 , 5 점은 원뿔 결정 , 선형 방정식의 시스템 , 행렬식
직교 평면에서 원뿔의 일반 방정식은 다음과 같습니다.
A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0
A 또는 B 또는 C가 0과 같지 않은 경우 (그렇지 않으면 직선 임)
A ... F는 6 개의 미지수입니다. 5 쌍의 (x, y)를 사용하면 5 개의 방정식으로 선형 시스템을 구축 할 수 있으며 스케일링은 1 차원을 제거합니다. 즉, 0이 아닌 경우 A, B 또는 C 중 하나를 1로 설정할 수 있습니다 (그리고 우리는 적어도 하나가 0이 아님을 알고 있습니다).
나는 3 개의 시스템을 구축하고 해결하려고 시도합니다. 해결할 수 없다면 B = 1, C입니다 (더 나은 방법이있을 수 있지만 그 당시에는 최선입니다)
A, B, C의 값을 가짐으로써 판별자를보고 원뿔을 분류 할 수 있습니다 d=B*B-4*A*C
- d == 0-> 포물선
- d> 0-> 쌍곡선
- d <0-> 타원, 특히 (A == C 및 B == 0)-> 원
덜 골프
F=p=>(
// Recursive function to find determinant of a square matrix
D=m=>m[1]
?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0)
:m,
// Try 3 linear systems, coefficients in Q
// Five equation made from the paramaters in p
// And a first equation with coefficient like k,0,0,0,0,0,1 (example for A)
[1,2,4].some(
x => (
// matrix to calc the determinant, last coefficient is missing at this stage
Q = [
[x&1, x&2, x&4, 0,0,0] // first one is different
// all other equations built from the params
,...p.map( ([x,y]) => [x*x, x*y, y*y, x, y, 1] )
],
d = D(Q), // here d is the determinant
d && ( // if solvable then d != 0
// add missing last coefficient to Q
// must be != 0 for the first row, must be 0 for the other
Q.map( r=> (r.push(x), x=0) ),
// solve the system (Cramer's rule), I get all values for A...F but I just care of a,b,c
[a,b,c] = Q.map((v,i)=>D(Q.map(r=>(r=[...r],r[i]=r.pop(),r))) / d),
d = b*b - 4*a*c, // now reuse d for discriminant
d = d<0 ? !b&c==a ? 'Circle' : 'Ellipse' // now reuse d for end result
: d ? 'Hyperbola' : 'Parabola'
) // exit .some if not 0
), d // .some exit with true, the result is in d
)
)
테스트
F=p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'
console.log=(...x)=>O.textContent+=x+'\n'
;[
[[0, 0], [1, 5], [2, 3], [4, 8], [9, 2]]
,[[1.2, 5.3],[4.1, 5.6], [9.1, 2.5], [0, 1], [4.2, 0]]
,[[5, 0], [4, 3], [3, 4], [0, 5], [0, -5]]
,[[1, 0], [0, 1], [2, 1], [3, 4], [4, 9]]
].forEach(t=>console.log(t.join`|`+' => '+F(t)))
<pre id=O></pre>
circle매우 둥근 타원형 구분하기 플로트 평등을 확인 요구하는 것 같다. 여기서 어떤 정밀도를 가정해야합니까?