एक बहुभुज के केंद्रक का पता लगाएं


16

से विकिपीडिया :

एक गैर-स्व-प्रतिच्छेदन बंद बहुभुज का केन्द्रक n कोने ( x 0 , y 0 ), ( x 1 , y 1 ), ..., ( x n - 1 , y n ) 1 ) द्वारा परिभाषित है। बिंदु ( C x , C y ), जहां

सेंट्रोइड के लिए सूत्र

और जहां A बहुभुज का हस्ताक्षरित क्षेत्र है,

बहुभुज के क्षेत्र के लिए सूत्र

इन सूत्रों में, बहुभुज की परिधि के साथ उनके घटने के क्रम में वर्टिकल गिने जाते हैं। इसके अलावा, शीर्ष ( x n , y n ) को ( x 0 , y 0 ) के समान माना जाता है , जिसका अर्थ है कि अंतिम मामले पर i + 1 को लगभग = i = 0 के बराबर होना चाहिए । ध्यान दें कि यदि बिंदुओं को दक्षिणावर्त क्रम में क्रमांकित किया जाता है तो क्षेत्र A , ऊपर के रूप में गणना की जाती है, एक नकारात्मक चिन्ह होगा; लेकिन केन्द्रक निर्देशांक इस मामले में भी सही होंगे।


  • क्रम में कोने की सूची को देखते हुए (या तो दक्षिणावर्त, या काउंटर-क्लॉकवाइज), कोने के द्वारा प्रतिनिधित्व किए गए गैर-स्व-प्रतिच्छेदन बंद बहुभुज का केंद्रक ज्ञात करें।
    • यदि यह मदद करता है, तो आप इनपुट को केवल CW, या केवल CCW मान सकते हैं। यदि आपको इसकी आवश्यकता है तो अपने उत्तर में कहें।
  • निर्देशांक पूर्णांक होना आवश्यक नहीं है, और इसमें नकारात्मक संख्याएँ हो सकती हैं।
  • इनपुट हमेशा मान्य होगा और इसमें कम से कम तीन कोने होंगे।
  • इनपुट को केवल आपकी भाषा के मूल फ़्लोटिंग पॉइंट डेटा प्रकार में फिट होने की आवश्यकता है।
  • आप मान सकते हैं कि इनपुट नंबर में हमेशा दशमलव बिंदु होगा।
  • आप मान सकते हैं कि इनपुट पूर्णांक अंत में .या.0
  • आप इनपुट के लिए जटिल संख्याओं का उपयोग कर सकते हैं।
  • आउटपुट निकटतम हजारवें तक सटीक होना चाहिए।

उदाहरण

[(0.,0.), (1.,0.), (1.,1.), (0.,1.)]        -> (0.5, 0.5)
[(-15.21,0.8), (10.1,-0.3), (-0.07,23.55)]  -> -1.727 8.017
[(-39.00,-55.94), (-56.08,-4.73), (-72.64,12.12), (-31.04,53.58), (-30.36,28.29), (17.96,59.17), (0.00,0.00), (10.00,0.00), (20.00,0.00), (148.63,114.32), (8.06,-41.04), (-41.25,34.43)]   -> 5.80104769975, 15.0673812762

एक समन्वय विमान पर प्रत्येक बहुभुज को देखें, इस पृष्ठ के "संपादित करें" मेनू में वर्ग कोष्ठक के बिना निर्देशांक चिपकाएं

मैंने इस बहुभुज सेंट्रोइड पॉइंट कैलकुलेटर का उपयोग करके अपने परिणामों की पुष्टि की , जो कि भयानक है। मुझे एक नहीं मिला कि आप एक ही बार में सभी कोने इनपुट कर सकते हैं, या -जब आप इसे पहले टाइप करते हैं तो अपने साइन को मिटाने की कोशिश नहीं की थी । लोगों को जवाब देने का मौका मिलने के बाद मैं आपके उपयोग के लिए अपना पायथन समाधान पोस्ट करूंगा।


पहले दो सेट के लिए सभी एक्स और वाई के कार्यों की औसत की बहुत सरल तकनीक, लेकिन तीसरे नहीं। मुझे आश्चर्य है कि क्या फर्क पड़ता है ...
ETHproductions

1
@ETHproductions तीसरा बहुभुज उत्तल नहीं है।
जुंगह्वान मिन ऑक्ट

1
@ETHproductions यदि आप बहुभुज के साथ एक वृत्त का अनुमान लगाते हैं, तो आप उस बिंदु के करीब अधिक बिंदुओं का उपयोग करके सर्कल पर एक बिंदु के करीब औसत बिंदु को मनमाने ढंग से स्थानांतरित कर सकते हैं, जबकि लगभग सेंट्रो को प्रभावित नहीं करते हैं और बहुभुज उत्तल रखते हैं।
क्रिश्चियन सिवर्स

2
@ETHproductions वास्तव में उत्तलता इसका कारण नहीं है। सभी xs और ys का लाभ शरीर पर वितरित होने के बजाय सभी भारों को वर्टिकल में डालता है। पहला काम करने के लिए होता है क्योंकि यह नियमित है, इसलिए दोनों तरीके समरूपता केंद्र पर समाप्त होते हैं। दूसरा काम करता है क्योंकि त्रिकोण के लिए दोनों विधियां एक ही बिंदु तक ले जाती हैं।
टन हास्पेल

1
क्या हम I / O के लिए जटिल संख्याओं का उपयोग कर सकते हैं?
xnor

जवाबों:


16

जेली , 25 24 22 21 18 बाइट्स

S×3÷@×"
ṙ-żµÆḊçS€S

समस्या में दिखाए गए सूत्र को लागू करता है।

@ जोनाथन एलन की मदद से 3 बाइट्स बचाए।

इसे ऑनलाइन आज़माएं! या सभी परीक्षण मामलों को सत्यापित करें।

व्याख्या

S×3÷@×"  Helper link. Input: determinants on LHS, sum of pairs on RHS
S        Sum the determinants
 ×3      Multiply by 3
     ×"  Vectorized multiply between determinants and sums
   ÷@    Divide that by the determinant sum multipled by 3 and return

ṙ-żµÆḊçS€S  Main link. Input: 2d list of points
ṙ-          Rotate the list of points by 1 to the right
  ż         Interleave those with the original points
            This creates all overlapping slices of length 2
   µ        Start new monadic chain
    ÆḊ      Get the determinant of each slice
       S€   Get the sum of each slice (sum of pairs of points)
      ç     Call the helper link
         S  Sum and return

आप बदल सकते हैं ṁL‘$ṡ2के साथ ṙ1ż@याżṙ1$
जोनाथन एलन

@JonathanAllan धन्यवाद, मैं भी ṙ-żस्वैप से बचने के लिए घुमा सकता हूं और एक और बाइट बचा सकता हूं
मील

ओह! हाँ क्यों नहीं!
जोनाथन एलन

17

गणितज्ञ, 23 बाइट्स

RegionCentroid@*Polygon

ले लो कि , जेली!

संपादित करें: एक बस जेली को हरा नहीं करता है ...

व्याख्या

Polygon

निर्दिष्ट बिंदुओं पर कोने के साथ एक बहुभुज बनाएं।

RegionCentroid

बहुभुज का केंद्रक ज्ञात करें।


2
वैसे आप मुझे हरा देते हैं, लेकिन मेरे पास जितना संभव है उससे कहीं कम है, मुझे जेली की पूरी समझ नहीं है
मील

3
@ मीलों जाग ...:
जंगवान मिन

4

जे, 29 बाइट्स

2+/@(+/\(*%3*1#.])-/ .*\)],{.

समस्या में दिखाए गए सूत्र को लागू करता है।

प्रयोग

   f =: 2+/@(+/\(*%3*1#.])-/ .*\)],{.
   f 0 0 , 1 0 , 1 1 ,: 0 1
0.5 0.5
   f _15.21 0.8 , 10.1 _0.3 ,: _0.07 23.55
_1.72667 8.01667
   f _39 _55.94 , _56.08 _4.73 , _72.64 12.12 , _31.04 53.58 , _30.36 28.29 , 17.96 59.17 , 0 0 , 10 0 , 20 0 , 148.63 114.32 , 8.06 _41.04 ,: _41.25 34.43
5.80105 15.0674

व्याख्या

2+/@(+/\(*%3*1#.])-/ .*\)],{.  Input: 2d array of points P [[x1 y1] [x2 y2] ...]
                           {.  Head of P
                         ]     Get P
                          ,    Join, makes the end cycle back to the front
2                              The constant 2
2                      \       For each pair of points
                  -/ .*        Take the determinant
2    +/\                       Sum each pair of points
         *                     Multiply the sum of each pair by its determinant
          %                    Divide each by
             1#.]              The sum of the determinants
           3*                  Multiplied by 3
 +/@                           Sum and return

4

मैक्सिमा, 124 118 116 112 106 बाइट

f(l):=(l:endcons(l[1],l),l:sum([3,l[i-1]+l[i]]*determinant(matrix(l[i-1],l[i])),i,2,length(l)),l[2]/l[1]);

मुझे मैक्सिमा का अनुभव नहीं है, इसलिए किसी भी संकेत का स्वागत है।

उपयोग:

(%i6) f([[-15.21,0.8], [10.1,-0.3], [-0.07,23.55]]);
(%o6)              [- 1.726666666666668, 8.016666666666668]

3

रैकेट 420 बाइट्स

(let*((lr list-ref)(getx(lambda(i)(lr(lr l i)0)))(gety(lambda(i)(lr(lr l i)1)))(n(length l))(j(λ(i)(if(= i(sub1 n))0(add1 i))))
(A(/(for/sum((i n))(-(*(getx i)(gety(j i)))(*(getx(j i))(gety i))))2))
(cx(/(for/sum((i n))(*(+(getx i)(getx(j i)))(-(*(getx i)(gety(j i)))(*(getx(j i))(gety i)))))(* 6 A)))
(cy(/(for/sum((i n))(*(+(gety i)(gety(j i)))(-(*(getx i)(gety(j i)))(*(getx(j i))(gety i)))))(* 6 A))))
(list cx cy))

Ungolfed:

(define(f l)
  (let* ((lr list-ref)
         (getx (lambda(i)(lr (lr l i)0)))
         (gety (lambda(i)(lr (lr l i)1)))
         (n (length l))
         (j (lambda(i) (if (= i (sub1 n)) 0 (add1 i))))
         (A (/(for/sum ((i n))
                (-(* (getx i) (gety (j i)))
                  (* (getx (j i)) (gety i))))
              2))
         (cx (/(for/sum ((i n))
                 (*(+(getx i)(getx (j i)))
                   (-(*(getx i)(gety (j i)))
                     (*(getx (j i))(gety i)))))
               (* 6 A)))
         (cy (/(for/sum ((i n))
                 (*(+(gety i)(gety (j i)))
                   (-(*(getx i)(gety (j i)))
                     (*(getx (j i))(gety i)))))
               (* 6 A))))
    (list cx cy)))

परिक्षण:

(f '[(-15.21 0.8)  (10.1 -0.3)  (-0.07 23.55)] ) 
(f '[(-39.00 -55.94)  (-56.08 -4.73)  (-72.64 12.12)  (-31.04 53.58) 
     (-30.36 28.29)  (17.96 59.17)  (0.00 0.00)  (10.00 0.00)  
     (20.00 0.00) (148.63 114.32)  (8.06 -41.04)  (-41.25 34.43)])

आउटपुट:

'(-1.7266666666666677 8.01666666666667)
'(5.8010476997538465 15.067381276150996)

3

आर, 129 127 बाइट्स

function(l){s=sapply;x=s(l,`[`,1);y=s(l,`[`,2);X=c(x[-1],x[1]);Y=c(y[-1],y[1]);p=x*Y-X*y;c(sum((x+X)*p),sum((y+Y)*p))/sum(p)/3}

फ़ंक्शन करें जो इनपुट के रूप में ट्यूपल्स की आर-सूची लेते हैं। नाम के समान का उपयोग करके बुलाया जा सकता है:

f(list(c(-15.21,0.8),c(10.1,-0.3),c(-0.07,23.55)))

अनगढ़ और समझाया गया

f=function(l){s=sapply;                           # Alias for sapply
              x=s(l,`[`,1);                       # Split list of tuples into vector of first elements
              y=s(l,`[`,2);                       # =||= but for second element 
              X=c(x[-1],x[1]);                    # Generate a vector for x(i+1)
              Y=c(y[-1],y[1]);                    # Generate a vector for y(i+1)
              p=x*Y-X*y;                          # Calculate the outer product used in both A, Cx and Cy
              c(sum((x+X)*p),sum((y+Y)*p))/sum(p)/3    # See post for explanation
}

अंतिम चरण ( c(sum((x+X)*p),sum((y+Y)*p))/sum(p)*2/6) दोनों की गणना का एक सदिश तरीका है Cxऔर Cy। के लिए फार्मूले में योग Cxऔर Cyएक सदिश में जमा हो जाती है और इसके फलस्वरूप "में योग से विभाजित A" *2/6। उदाहरण के लिए:

(SUMinCx, SUMinCy) / SUMinA / 3

, और फिर स्पष्ट रूप से मुद्रित।

इसे आर-फिडल पर आज़माएं


*2/6शायद हो सकता है /3?
mbomb007

@ mbomb007 यह बहुत ही स्पष्ट रूप से स्पष्ट है, मुझे लगता है कि मैंने दूसरे भाग को गोल्फ में पकड़ लिया है। /
श्रग

सुरुचिपूर्ण, मुझे sapplyउन सूचियों से निपटने के लिए आपका उपयोग पसंद है ! यहां गोल्फिंग की गुंजाइश हो सकती है, मुझे यकीन नहीं है कि स्वीकार्य इनपुट कितना लचीला है। यदि आपको निर्देशांक के एक अनुक्रम को इनपुट करने की अनुमति है, जैसे c(-15.21,0.8,10.1,-0.3,-0.07,23.55), तो आप अपने फ़ंक्शन की पहली पंक्तियों को बदलकर 17 बाइट्स बचा सकते हैं y=l[s<-seq(2,sum(1|l),2)];x=l[-s];। अर्थात, yप्रत्येक सम-अनुक्रमित तत्व lऔर xप्रत्येक विषम-अनुक्रमित तत्व होने के लिए सेटिंग ।
rturnbull

हालांकि, इससे भी बेहतर, अगर हम एक मैट्रिक्स (या सरणी) इनपुट कर सकते हैं, जैसे matrix(c(-15.21,0.8,10.1,-0.3,-0.07,23.55),2), तब तक आपके फ़ंक्शन की शुरुआत हो सकती है x=l[1,];y=l[2,];, जो 35 बाइट्स बचाता है। (इनपुट मैट्रिक्स को ट्रांसपोज़ किया जा सकता है, जिस स्थिति में x=l[,1];y=l[,2];।) बेशक, सभी का सबसे आसान समाधान है अगर xऔर yपॉइंट्स अलग-अलग वैक्टर के रूप में इनपुट हैं function(x,y), लेकिन मुझे नहीं लगता कि इसकी अनुमति है ...
rturnbull

@rturnbull मैंने टिप्पणियों में ओपी से पूछा और वह विशेष रूप से ट्यूपल्स की सूची चाहता था (आर के पाठ्यक्रम में बहुत असुविधाजनक) इसलिए मुझे नहीं लगता कि मैट्रिक्स दृष्टिकोण की अनुमति है। और यहां तक ​​कि अगर यह था, तो इनपुट को वेक्टर भाग (यानी c(...)) होना चाहिए और मैट्रिक्स रूपांतरण फ़ंक्शन के अंदर करना होगा।
बिलीवोब

2

पायथन, 156 127 बाइट्स

def f(p):n=len(p);p=p+p[:1];i=s=0;exec'd=(p[i].conjugate()*p[i+1]).imag;s+=d;p[i]=(p[i]+p[i+1])*d;i+=1;'*n;print sum(p[:n])/s/3

Ungolfed:

def f(points):
  n = len(points)
  points = points + [points[0]]
  determinantSum = 0
  for i in range(n):
    determinant = (points[i].conjugate() * points[i+1]).imag
    determinantSum += determinant
    points[i] = (points[i] + points[i+1]) * determinant
  print sum(points[:n]) / determinantSum / 3

Ideone यह।

यह प्रत्येक जोड़ी [x, y]को एक जटिल संख्या के रूप में लेता है x + y*j, और परिणामी सेंट्रोइड को एक ही प्रारूप में एक जटिल संख्या के रूप में आउटपुट करता है।

बिंदुओं की जोड़ी के लिए [a, b]और [c, d], a*d - b*cप्रत्येक जोड़ी बिंदुओं के लिए आवश्यक मूल्य को मैट्रिक्स के निर्धारक से गणना की जा सकती है

| a b |
| c d |

जटिल अंकगणितीय, जटिल मूल्यों का उपयोग करना a + b*jऔर c + d*jइसका उपयोग किया जा सकता है

conjugate(a + b*j) * (c + d*j)
(a - b*j) * (c + d*j)
(a*c + b*d) + (a*d - b*c)*j

ध्यान दें कि काल्पनिक हिस्सा नियतांक के बराबर है। इसके अलावा, जटिल मूल्यों का उपयोग करने से अंक आसानी से अन्य कार्यों में घटक-वार को अभिव्यक्त करने की अनुमति देता है।


2

आर + एसपी (46 बाइट्स)

मान लिया जाता है कि spपैकेज स्थापित है ( https://cran.r-project.org/web/packages/sp/ )

शीर्षकों की एक सूची लेता है, (उदाहरण के लिए list(c(0.,0.), c(1.,0.), c(1.,1.), c(0.,1.)))

इस तथ्य का लाभ उठाता है कि बहुभुज का "लैप्ट" सेंट्रोइड है।

function(l)sp::Polygon(do.call(rbind,l))@labpt

2

जावास्क्रिप्ट (ईएस 6), 102

सूत्र का सीधा कार्यान्वयन

l=>[...l,l[0]].map(([x,y],i)=>(i?(a+=w=t*y-x*u,X+=(t+x)*w,Y+=(u+y)*w):X=Y=a=0,t=x,u=y))&&[X/3/a,Y/3/a]

परीक्षा

f=
l=>[...l,l[0]].map(([x,y],i)=>(i?(a+=w=t*y-x*u,X+=(t+x)*w,Y+=(u+y)*w):X=Y=a=0,t=x,u=y))&&[X/3/a,Y/3/a]

function go()
{
  var c=[],cx,cy;
  // build coordinates array
  I.value.match(/-?[\d.]+/g).map((v,i)=>i&1?t[1]=+v:c.push(t=[+v]));
  console.log(c+''),
  [cx,cy]=f(c);
  O.textContent='CX:'+cx+' CY:'+cy;
  // try to display the polygon
  var mx=Math.max(...c.map(v=>v[0])),
    nx=Math.min(...c.map(v=>v[0])),
    my=Math.max(...c.map(v=>v[1])),
    ny=Math.min(...c.map(v=>v[1])),  
    dx=mx-nx, dy=my-ny,
    ctx=C.getContext("2d"),
    cw=C.width, ch=C.height,
    fx=(mx-nx)/cw, fy=(my-ny)/ch, fs=Math.max(fx,fy)
  C.width=cw
  ctx.setTransform(1,0,0,1,0,0);
  ctx.beginPath();
  c.forEach(([x,y],i)=>ctx.lineTo((x-nx)/fs,(y-ny)/fs));
  ctx.closePath();
  ctx.stroke();
  ctx.fillStyle='#ff0000';
  ctx.fillRect((cx-nx)/fs-2,(cy-ny)/fs-2,5,5);
}
go()
#I { width:90% }
#C { width:90%; height:200px;}
<input id=I value='[[-15.21,0.8], [10.1,-0.3], [-0.07,23.55]]'>
<button onclick='go()'>GO</button>
<pre id=O></pre>
<canvas id=C></canvas>


1

पायथन 2, 153 बाइट्स

कोई जटिल संख्या का उपयोग करता है।

P=input()
A=x=y=0;n=len(P)
for i in range(n):m=-~i%n;a=P[i][0];b=P[i][1];c=P[m][0];d=P[m][1];t=a*d-b*c;A+=t;x+=t*(a+c);y+=t*(b+d)
k=1/(3*A);print x*k,y*k

इसे ऑनलाइन आज़माएं

Ungolfed:

def centroid(P):
    A=x=y=0
    n=len(P)
    for i in range(n):
        m=-~i%n
        x0=P[i][0];y0=P[i][1]
        x1=P[m][0];y1=P[m][1]
        t = x0*y1 - y0*x1
        A += t/2.
        x += t * (x0 + x1)
        y += t * (y0 + y1)
    k = 1/(6*A)
    x *= k
    y *= k
    return x,y

1

दरअसल, 45 40 39 बाइट्स

यह मील के जेली उत्तर के समान एल्गोरिथ्म का उपयोग करता है । एक डॉट उत्पाद का उपयोग करके निर्धारकों की गणना करने का एक छोटा तरीका है, लेकिन वर्तमान में वास्तव में डॉट उत्पाद के साथ एक बग है जहां यह फ़्लोट की सूचियों के साथ काम नहीं करेगा। गोल्फ सुझाव का स्वागत करते हैं। इसे ऑनलाइन आज़माएं!

;\Z♂#;`i¥`M@`i│N@F*)F@N*-`M;Σ3*)♀*┬♂Σ♀/

Ungolfing

         Implicit input pts.
;\       Duplicate pts, rotate right.
Z        Zip rot_pts and pts together.
♂#       Convert the iterables inside the zip to lists
         (currently necessary due to a bug with duplicate)
;        Duplicate the zip.
`...`M   Get the sum each pair of points in the zip.
  i        Flatten the pair to the stack.
  ¥        Pairwise add the two coordinate vectors.
@        Swap with the other zip.
`...`M   Get the determinants of the zip.
  i│       Flatten to stack and duplicate entire stack.
           Stack: [a,b], [c,d], [a,b], [c,d]
  N@F*)    Push b*c and move it to BOS.
  F@N*     Push a*d.
  -        Get a*d-b*c.
;Σ3*)    Push 3 * sum(determinants) and move it to BOS.
♀*       Vector multiply the determinants and the sums.
┬        Transpose the coordinate pairs in the vector.
♂Σ       Sum the x's, then the y's.
♀/       Divide the x and y of this last coordinate pair by 3*sum(determinants).
         Implicit return.

एक छोटा, गैर-प्रतिस्पर्धी संस्करण

यह एक और 24-बाइट संस्करण है जो जटिल संख्याओं का उपयोग करता है। यह गैर-प्रतिस्पर्धी है क्योंकि यह बग फिक्स पर निर्भर करता है जो इस चुनौती को स्थगित करता है। इसे ऑनलाइन आज़माएं!

;\│¥)Z`iá*╫@X`M;Σ3*)♀*Σ/

Ungolfing

         Implicit input a list of complex numbers, pts.
;\       Duplicate pts, rotate right.
│        Duplicate stack. Stack: rot_pts, pts, rot_pts, pts.
¥)       Pairwise sum the two lists of points together and rotate to BOS.
Z        Zip rot_pts and pts together.
`...`M   Map the following function over the zipped points to get our determinants.
  i        Flatten the list of [a+b*i, c+d*i].
  á        Push the complex conjugate of a+bi, i.e. a-b*i.
  *        Multiply a-b*i by c+d*i, getting (a*c+b*d)+(a*d-b*c)*i.
           Our determinant is the imaginary part of this result.
  ╫@X      Push Re(z), Im(z) to the stack, and immediately discard Re(z).
           This map returns a list of these determinants.
;        Duplicate list_determinants.
Σ3*)     Push 3 * sum(list_determinants) and rotate that to BOS.
♀*Σ      Pairwise multiply the sums of pairs of points and the determinants and sum.
/        Divide that sum by 3*sum(list_determinants).
         Implicit return.

1

सी ++ 14, 241 बाइट्स

struct P{float x;float y;};
#define S(N,T)auto N(P){return 0;}auto N(P a,P b,auto...V){return(T)*(a.x*b.y-b.x*a.y)+N(b,V...);}
S(A,1)S(X,a.x+b.x)S(Y,a.y+b.y)auto f(auto q,auto...p){auto a=A(q,p...,q)*3;return P{X(q,p...,q)/a,Y(q,p...,q)/a};}

आउटपुट सहायक संरचना है P,

Ungolfed:

 //helper struct
struct P{float x;float y;};

//Area, Cx and Cy are quite similar
#define S(N,T)\  //N is the function name, T is the term in the sum
auto N(P){return 0;} \   //end of recursion for only 1 element
auto N(P a,P b,auto...V){ \ //extract the first two elements
  return (T)*(a.x*b.y-b.x*a.y) //compute with a and b
         + N(b,V...); \        //recursion without first element
}

//instantiate the 3 formulas
S(A,1)
S(X,a.x+b.x)
S(Y,a.y+b.y)


auto f(auto q,auto...p){
  auto a=A(q,p...,q)*3; //q,p...,q appends the first element to the end
  return P{X(q,p...,q)/a,Y(q,p...,q)/a};
}

उपयोग:

f(P{0.,0.}, P{1.,0.}, P{1.,1.}, P{0.,1.})
f(P{-15.21,0.8}, P{10.1,-0.3}, P{-0.07,23.55})

1

क्लोजर, 177 156 143 बाइट्स

अद्यतन: एक कॉलबैक के बजाय मैं [a b c d 1]एक फ़ंक्शन के रूप में उपयोग कर रहा हूं और तर्क इस वेक्टर में अनुक्रमित सूची है। 1गणना करते समय एक प्रहरी मूल्य के रूप में उपयोग किया जाता है A

अद्यतन 2: precalculating नहीं Aपर let, का उपयोग करते हुए (rest(cycle %))इनपुट वैक्टर एक की भरपाई के लिए।

#(let[F(fn[I](apply +(map(fn[[a b][c d]](*(apply +(map[a b c d 1]I))(-(* a d)(* c b))))%(rest(cycle %)))))](for[i[[0 2][1 3]]](/(F i)(F[4])3)))

मूल संस्करण:

#(let[F(fn[L](apply +(map(fn[[a b][c d]](*(L[a b c d])(-(* a d)(* c b))))%(conj(subvec % 1)(% 0)))))A(*(F(fn[& l]1))3)](map F[(fn[v](/(+(v 0)(v 2))A))(fn[v](/(+(v 1)(v 3))A))]))

कम गोल्फ वाले चरण में:

(def f (fn[v](let[F (fn[l](apply +(map
                                    (fn[[a b][c d]](*(l a b c d)(-(* a d)(* c b))))
                                    v
                                    (conj(subvec v 1)(v 0)))))
                  A (* (F(fn[& l] 1)) 3)]
                [(F (fn[a b c d](/(+ a c)A)))
                 (F (fn[a b c d](/(+ b d)A)))])))

एक सहायक फ़ंक्शन बनाता है Fजो किसी भी कॉलबैक के साथ योग को लागू करता है lAकॉलबैक के लिए लगातार रिटर्न 1जबकि एक्स और वाई निर्देशांक के अपने कार्य हैं। (conj(subvec v 1)(v 0))पहले तत्व को छोड़ता है और अंत तक अपील करता है, इस तरह से ट्रैक करना आसान है x_iऔर x_(i+1)। हो सकता है कि अभी भी कुछ पुनरावृत्ति को समाप्त किया जाना है, खासकर आखिरी में (map F[...

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.