用石英二维画一座桥

时间:2023-02-09 08:39:19

I'm trying to draw the following image:

我试着画下面的图像:

用石英二维画一座桥

Using this objective-c code:

使用objective - c代码:

CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
CGContextMoveToPoint(ctx, position.X - (size.width / 2), position.Y - (size.height / 2));
CGContextAddLineToPoint(ctx, position.X - (size.width / 2), position.Y + (size.height / 2));
CGContextAddLineToPoint(ctx, position.X - (size.width / 4), position.Y + (size.height / 2));
CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, M_PI, 0);
CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y + (size.height / 2));
CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y - (size.height / 2));
CGContextFillPath(ctx);

But it doesn't work. I get this image:

但它不工作。我得到这张图片:

用石英二维画一座桥

The width of the image is size.width and the height is size.height. The origin is (position.X, position.Y). This point is in center, at (size.width /2, size.height / 2).

图像的宽度是大小。宽度和高度是尺寸。高度。原点(位置。X,position.Y)。这个点在中心,在(大小)。宽/ 2,大小。高度/ 2)。

The first point is the upper left corner, and the second one is the bottom left corner. And then continues to the right.

第一个点是左上角,第二个点是左下角。然后继续向右。

Here is a better explanation: 用石英二维画一座桥

这里有一个更好的解释:

Sorry for my English.

对不起,我的英语。

Any advice?

任何建议吗?

3 个解决方案

#1


2  

I've changed this line:

我已经改变这条线:

CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, M_PI, 0);

With the following:

用以下:

CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, 0, 0);

And now it works!!! I had an error with the second angle. Instead of M_PI, the correct is 0.

现在工作! ! !第二个角有误差。而不是M_PI,正确的是0。

#2


0  

You stated: The width of the image is size.width and the height is size.height. The origin is (position.X, position.Y), and this point is in center, at (size.width /2, size.height / 2).

你说:图像的宽度是大小。宽度和高度是尺寸。高度。原点(位置。X,位置。y,这个点在中心,在大小。宽/ 2,大小。高度/ 2)。

First thought is that in general, your center point is actually at (position.X + size.width/2.0, position.Y + size.width/2.0), (wrapping that in floorf if you desire).

首先,一般来说,你的中心点实际上是在(位置)。2.0 X +宽度/高度,位置。Y + size.width/2.0),(如果你愿意,可以把它放在floorf中)。

If that doesn't work, my advice is to create CGPoint, CGRect, etc, for every intermediate value to make all of your calculations explicit. Then if need be, you can NSLog them, find out exactly where your calculations go wrong.

如果不行,我的建议是为每一个中间值创建CGPoint、CGRect等,使所有计算都显式。如果需要的话,你可以用NSLog,找出你的计算错误的地方。

#3


0  

Does reversing your point draw order fix it?

倒转你的点画顺序能修正它吗?

#1


2  

I've changed this line:

我已经改变这条线:

CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, M_PI, 0);

With the following:

用以下:

CGContextAddArc(ctx, position.X, position.Y + (size.height / 2), (size.width / 4), -M_PI, 0, 0);

And now it works!!! I had an error with the second angle. Instead of M_PI, the correct is 0.

现在工作! ! !第二个角有误差。而不是M_PI,正确的是0。

#2


0  

You stated: The width of the image is size.width and the height is size.height. The origin is (position.X, position.Y), and this point is in center, at (size.width /2, size.height / 2).

你说:图像的宽度是大小。宽度和高度是尺寸。高度。原点(位置。X,位置。y,这个点在中心,在大小。宽/ 2,大小。高度/ 2)。

First thought is that in general, your center point is actually at (position.X + size.width/2.0, position.Y + size.width/2.0), (wrapping that in floorf if you desire).

首先,一般来说,你的中心点实际上是在(位置)。2.0 X +宽度/高度,位置。Y + size.width/2.0),(如果你愿意,可以把它放在floorf中)。

If that doesn't work, my advice is to create CGPoint, CGRect, etc, for every intermediate value to make all of your calculations explicit. Then if need be, you can NSLog them, find out exactly where your calculations go wrong.

如果不行,我的建议是为每一个中间值创建CGPoint、CGRect等,使所有计算都显式。如果需要的话,你可以用NSLog,找出你的计算错误的地方。

#3


0  

Does reversing your point draw order fix it?

倒转你的点画顺序能修正它吗?