我在Uni有一项任务,它涉及绘制许多不同的形状,所有的图形都必须用C语言的gdImage库绘制。到目前为止,我使用了gdImageLine和gdImageRectangle,如下所示:
gdImageLine ( gdImage, 150, 70, 170, 90, blue);或
gdImageRectangle( gdImage, 110, 80, 160, 120, blue);我非常缺乏经验/对C一无所知,所以任何帮助都是很棒的!
嗨,抱歉我搞糊涂了。我想用"gdImagePolygon“的方式画一个形状/类似于我如何使用另外两个,如果这有意义的话?我收到了这个链接(reference.html),谢谢
发布于 2013-12-17 18:51:04
gdImagePolygon具有以下签名:
gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)gdImagePtr是指向gdImage结构的指针。
gdPointPtr是指向gdPoint结构 (点的两个ints、x和y)的指针:
typedef struct {
int x, y;
} gdPoint, *gdPointPtr;pointsTotal是你的总点数(最少3点)。
color是颜色
绘制三角形的示例:
... inside a function ...
gdImagePtr im;
int black;
int white;
/* Points of polygon */
gdPoint points[3]; // an array of gdPoint structures is used here
im = gdImageCreate(100, 100);
/* Background color (first allocated) */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and
blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a triangle. */
points[0].x = 50;
points[0].y = 0;
points[1].x = 99;
points[1].y = 99;
points[2].x = 0;
points[2].y = 99;
gdImagePolygon(im, points, 3, white);
/* ... Do something with the image, such as
saving it to a file... */
/* Destroy it */
gdImageDestroy(im);发布于 2013-12-17 18:46:29
我从头到尾写了这个代码片段,如果有任何错误,请注意。谢谢。
int Draw_Polygon (int Side, ...)
{
va_list Ap;
va_start (Ap, Side);
int X_1 = va_arg (Ap, int);
int X_0 = X_1;
int Y_1 = va_arg (Ap, int);
int Y_0 = Y_1;
int Cnt;
for (Cnt = 0; Cnt < Side-1; Cnt++)
{
int X_2 = va_arg (Ap, int);
int Y_2 = va_arg (Ap, int);
gdImageLine ( gdImage, X_1, Y_1, X_2, Y_2, blue);
X_1 = X_2;
Y_1 = Y_2;
}
gdImageLine ( gdImage, X_1, Y_1, X_0, Y_0, blue);
va_end(Ap);
return 0;
}
int main (void)
{
if (Draw_Polygon (5, 0, 0, 0, 10, 12, 12, 16, 8, 5, 0) == 0) // Draw a pentagon.
{
// Success !
}
if (Draw_Polygon (6, 0, 0, 0, 10, 12, 12, 16, 8, 6, 3, 5, 0) == 0) // Draw a hexagon.
{
// Success !
}
}发布于 2013-12-17 18:44:44
快速伪码绘制N边的多边形,边长为L
Procedure DrawPol (integer N,L)
Integer i
For i=1 To N
Draw (L)
Turn (360/N)
EndFor
EndProcedure此伪代码基于两个原语,它们在徽标等语言中很常见:
Draw (L):在当前方向绘制一条L像素线Turn (A):通过向当前方向添加A度来更改当前方向要使用Draw函数和Turn函数实现Line和Turn,您可以使用如下所示:
Real CurrentAngle = 0 /* global variable */
Integer CurrentX = MAXX / 2 /* last point drawn */
Integer CurrentY = MAXY / 2 /* initialized to the center of the paint area */
Procedure Draw (Integer L)
Integer FinalX,FinalY
FinalX = CurrentX + L*cos(CurrentAngle)
FinalY = CurrentY + L*sin(CurrentAngle)
Line (CurrentX, CurrentY, FinalX, FinalY) /* gdImageLine() function actually */
CurrentX = FinalX
CurrentY = FinalY
EndProcedure
Procedure Turn (Float A)
CurrentAngle = CurrentAngle + A
If (CurrentAngle>360) /* MOD operator usually works */
CurrentAngle = 360-CurrentAngle /* only for integers */
EndIf
EndProcedurehttps://stackoverflow.com/questions/20641774
复制相似问题