كيفية رسم المضلعات مع cgpath؟
-
20-09-2019 - |
سؤال
لقد قرأت من خلال الوثائق ولكنها ليست واضحة على الفور بالنسبة لي كيفية رسم مضلع باستخدام cgpath. كل ما أحتاج إليه هو رسم cgpath حول شيء مثل هذا:
__
\ \
\ \
\__\
هل يمكن لأي شخص تقديم مقتطف حول كيفية القيام بذلك؟
بالإضافة إلى ذلك، أفترض أن cgpathcontainspoint سيساعدني في تحديد ما إذا كانت هناك نقطة داخل المسار؟، أو هل يجب أن يكون المسار رسم صلب
أيضا كيف يمكنني نقل cgpath حولها؟ هل هذا سهل مثل تغيير شيء مثل الأصل مثل في cgrect؟
شكرًا لك.
-oscar.
المحلول
يجب أن تفعل ذلك مثل هذا:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
for(int idx = 0; idx < self.points.count; idx++)
{
point = [self.points objectAtIndex:idx];//Edited
if(idx == 0)
{
// move to the first point
CGContextMoveToPoint(context, point.x, point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, point.y);
}
}
CGContextStrokePath(context);
}
ملاحظة هنا، النقاط هي صفيف النقاط التي تريد رسم المضلع ل. لذلك يجب أن يكون طريق دائري مثل: أنت ترسم مثلث النقاط (x1, x2, x3)
ثم يجب أن تمر إلى الصفيف (x1, x2, x3, x1)
.
أتمنى أن يساعدك هذا.
نصائح أخرى
هذا مثال على كيفية إنشاء مثلث باستخدام CgPath، عليك فقط وضع النقاط.
var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0) //start from here
CGPathAddLineToPoint(path, nil, 20, 44)
CGPathAddLineToPoint(path, nil, 40, 0)
CGPathAddLineToPoint(path, nil, 0, 0)
//and to use in SpriteKit, for example
var tri = SKShapeNode(path: path)
var color = NSColor.blueColor()
tri.strokeColor = color
tri.fillColor = color
هذه هي النتيجة
انظر أبل كوارتزمو تطبيق. لديها رمز للقيام بذلك، وكذلك العديد من وظائف رسم الكوارتز الأخرى.
درايلاخ أجب محدثة SWIFT 4:
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 20, y: 44))
path.addLine(to: CGPoint(x: 40, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))
let tri = SKShapeNode(path: path)
كان من فئة CS193P ستانفورد على iPhone مشروع يسمى Hellopoly قد يكون بالضبط ما تريد - انظر الصف الصفحة الرئيسية بالنسبة للمواصفات ثم انظر الفيديو لكيفية تنفيذها (وحلول Google من الأشخاص الذين قاموا بالمهمة).