2012-01-03 5 views
16

आप एक चाप क्वार्ट्ज का उपयोग कर आकर्षित करने के लिए निम्नलिखित कोड का उपयोग कर सकते हैं:CGContextAddArcToPoint() का उपयोग करके एक चाप ड्राइंग करते समय, (x1, y1) और (x2, y2) का क्या अर्थ है?

CGContextMoveToPoint(context2, x, y); 
CGContextAddArcToPoint(context2, x1, y1, x2, y2, r); 

इन कार्यों में, (x,y) प्रारंभिक बिंदु है और r चाप त्रिज्या है लेकिन (x1,y1) और (x2,y2) क्या हैं?

+0

क्या http://developer.apple.com/library/IOs/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html इसे समझा नहीं है? वास्तविक सवाल, यदि आप उसमें नहीं हैं तो यह थोड़ा सा गणित भारी है। – jrturton

उत्तर

10

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextAddArcToPoint

x1: एक्स-मूल्य, उपयोगकर्ता अंतरिक्ष निर्देशांक में, पहले स्पर्श पंक्ति के अंत बिंदु के लिए। पहली टेंगेंट लाइन वर्तमान बिंदु से (x1, y1) तक खींची जाती है।

y1: वाई-वैल्यू, उपयोगकर्ता स्पेस निर्देशांक में, पहले टेंगेंट लाइन के अंत बिंदु के लिए। पहली टेंगेंट लाइन वर्तमान बिंदु से (x1, y1) तक खींची जाती है।

x2: द्वितीय टेंगेंट लाइन के अंत बिंदु के लिए, उपयोगकर्ता स्पेस निर्देशांक में एक्स-वैल्यू। दूसरी स्पर्श रेखा को (x1, y1) से (x2, y2) से खींचा जाता है।

y2: वाई-वैल्यू, उपयोगकर्ता अंतरिक्ष समन्वय में, दूसरी स्पर्श रेखा के अंत बिंदु के लिए। दूसरी स्पर्श रेखा को (x1, y1) से (x2, y2) से खींचा जाता है।

+2

जेम्स स्नूक द्वारा नीचे दिए गए उत्तर में एक स्पष्ट स्पष्ट स्पष्टीकरण है। – OutOnAWeekend

6

यहाँ केंद्र के- चक्र के नजरिए से यह आ रहा, घोषणाओं और नमूना मूल्यों के साथ कोड मैं सिर्फ इस हल करने के लिए बनाया गया है:

CGPoint arcCenter = CGPointMake(30,20); 
float arcLengthRad = M_PI_4; // Whatever, the full span of the arc in radians 
float radius = 10; 
float arcCenterRad = M_PI_2; // the angle of the center of the arc, in radians 

float arcP1hyp = 1/cos(arcLengthRad/2) * radius; 
float arcP1x = arcCenter.x + cosf(arcCenterRad)*arcP1hyp; 
float arcP1y = arcCenter.y + sinf(arcCenterRad)*arcP1hyp; 
float arcP2tx = arcCenter.x + cosf(arcCenterRad+(arcLengthRad/2))*radius; 
float arcP2ty = arcCenter.y + sinf(arcCenterRad+(arcLengthRad/2))*radius; 
float arcP2x = (arcP1x - arcP2tx)*-1 + arcP2tx; 
float arcP2y = (arcP1y - arcP2ty)*-1 + arcP2ty; 
CGContextAddArcToPoint(context, 
         arcP1x, 
         arcP1y, 
         arcP2x, 
         arcP2y, 
         radius); 

तो उपरोक्त कोड एक छोटी सी, 45 डिग्री के कोण का उत्पादन करना चाहिए एक सर्कल के शीर्ष पर चाप।


संपादित: एक टिप्पणी के जवाब में प्राप्त किया, ऊपर सूचीबद्ध सुपर संक्षिप्त कोड नीचे टिप्पणी के साथ दिखाया गया है, और एक विधि (प्लस arcP2 गणना करने के लिए एक छोटी सी समायोजन)

में लिपटे
/* 
EOTContext:addArcWithCenter:arcLength:radius:arcMiddlePointAngle: 

Use this method for building a circle with breaks at certain points, 
for example to use other CGContext methods to draw notches in the 
circle, or protruding points like gear teeth. 

This method builds up the values to use in CGContextAddArcToPoint(), 
which are the x and y coordinates of two points. First added to 
the current point in context, form two lines that are the tangents of 
the entry and exit angles of the arc. 

This method's arguments define the length of the arc in radians, and 
the position of start and end using the angle centerpoint of the arc. 
This is useful when drawing a certain defined amount of gear teeth, 
rotating around the circle. 

It is beyond this method's scope to maintain or calculate the 
centerpoint relative to an arbitrary current point in the context, because this 
is primarily used for drawing a gear/notch circle. 
*/ 
-(void)EOTContext:(CGContext*)context 
addArcWithCenter:(CGPoint)arcCenter 
arcLength:(CGFloat)arcLengthRad 
radius:(CGFloat)radius 
arcMiddlePointAngle:(CGFloat)arcCenterRad { 



    /* 
    Calculate the hypotenuse of the larger, outer circle where the 
    points of the tangent lines would rest upon (imagine wrapping 
    the drawn circle in a bounding regular convex polygon of tangent 
    lines, then wrap that polygon in an outer circle) 
    */ 
    float arcP1hyp = 1/cos(arcLengthRad/2) * radius; 

    // Build first tangent point 
    CGPoint arcP1 = (CGPoint){ 
     arcCenter.x + cosf(arcCenterRad)*arcP1hyp, 
     arcCenter.y + sinf(arcCenterRad)*arcP1hyp 
    }; 

    // Build the final endpoint of the arc 
    CGPoint arcP2final = (CGPoint){ 
     arcCenter.x + cosf(arcCenterRad+(arcLengthRad/2))*radius, 
     arcCenter.y + sinf(arcCenterRad+(arcLengthRad/2))*radius 
    }; 

    // Build second tangent point using the first tangent point and the final point of the arc. 
    // This point is resting on the bounding outer circle like arcP1 is. 
    // This would also work using the final point itself, using the simple assignment of arcP2 = arcP2final; 
    // or of course simply omitting arcP2 altogether. 
    CGPoint arcP2 = (CGPoint){ 
     (arcP2final.x - arcP1.x) + arcP2final.x, 
     (arcP2final.y - arcP1.y) + arcP2final.y 
    }; 

    // The following adds an arc of a circle to the current path, using a radius and tangent points. 
    CGContextAddArcToPoint(context, 
          arcP1.x, 
          arcP1.y, 
          arcP2.x, 
          arcP2.y, 
          radius); 
} 
+0

क्या आप कृपया बता सकते हैं कि आपने गणना कैसे की? – Moxy

+0

जब मैंने इसकी गणना की, तो मेरे पास एक नोट पैड पर लिखा गणित का विवरण था, लेकिन यह सब काम करने के बाद पेपर बाहर फेंक दिया! लेकिन यह बहुत चुनौतीपूर्ण नहीं है। बस मेरे विशिष्ट कोड का उपयोग करने के लिए संदर्भ जानना महत्वपूर्ण है। यह एक सर्कल ड्राइंग के लिए उपयोगी है, इसे आर्क की श्रृंखला के रूप में खींचकर, या गियर ड्राइंग के साथ, एक गियर ड्राइंग करके, लेकिन ऊपर या दांत ड्राइंग उपरोक्त कोड के दायरे से बाहर है। –

+0

कोड में टिप्पणी जोड़ा गया। बहुत बुरा सवाल पहले से ही उत्तर दिया गया है। –

65

AddArcToPoint इस तरह काम करता है:

ArcToPoint Diagram

जहां P1 वह बिंदु है जिस पर वर्तमान में पथ है, r फ़ंक्शन को दिए गए radius है, और लाल रेखा वह रेखा है जो addArcToPoint वर्तमान पथ में जोड़ देगा। यह x2, y2 पर दूसरे बिंदु पर जारी नहीं रहेगा; यह चाप के अंत में रुक जाएगा।

मेरे पास इस here के बारे में एक ब्लॉग पोस्ट है।

+0

स्पष्ट रूप से मैं यहां छवि को स्वयं नहीं जोड़ सकता क्योंकि मेरे पास पर्याप्त पर्याप्त प्रतिनिधि नहीं है। माफ़ कीजिये। –

+0

अच्छी छवि। मैंने इसे एक एम्बेडेड किया है लेकिन आपको अपना अगला करने में सक्षम होना चाहिए (जब तक मुझे अनुमतियों को सही तरीके से याद न हो)। +1 –

+0

उत्कृष्ट स्पष्टीकरण। क्वार्ट्ज 2 डी गाइड में चित्रण बहुत समझ में नहीं आता है, यह बहुत स्पष्ट है। – OutOnAWeekend

0

मैं सेब दस्तावेज संक्षेप में वर्णित है।

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextAddArcToPoint

x1: एक्स-मूल्य, उपयोगकर्ता अंतरिक्ष निर्देशांक में, पहले स्पर्श पंक्ति के अंत बिंदु के लिए। पहली टेंगेंट लाइन वर्तमान बिंदु से (x1, y1) तक खींची जाती है।

वाई 1: वाई-वैल्यू, उपयोगकर्ता स्पेस निर्देशांक में, पहले टेंगेंट लाइन के अंत बिंदु के लिए। पहली टेंगेंट लाइन वर्तमान बिंदु से (x1, y1) तक खींची जाती है।

x2: द्वितीय टेंगेंट लाइन के अंत बिंदु के लिए, उपयोगकर्ता स्पेस निर्देशांक में एक्स-वैल्यू। दूसरी स्पर्श रेखा को (x1, y1) से (x2, y2) से खींचा जाता है।

y2: वाई-वैल्यू, उपयोगकर्ता अंतरिक्ष समन्वय में, दूसरी स्पर्श रेखा के अंत बिंदु के लिए। दूसरी स्पर्श रेखा को (x1, y1) से (x2, y2) से खींचा जाता है।

संबंधित मुद्दे