我在Uni有一个任务,涉及绘制许多不同的形状,所有这些都必须使用C语言的gdImage库绘制。到目前为止,我已使用gdImageLine
和gdImageRectangle
,如下所示:
gdImageLine ( gdImage, 150, 70, 170, 90, blue);
或
gdImageRectangle( gdImage, 110, 80, 160, 120, blue);
我非常缺乏经验/对C没有任何线索,所以任何帮助都会很棒!
嗨,抱歉我感到困惑。我想使用“gdImagePolygon”绘制一个形状/类似于我如何使用其他两个,如果这有意义的话?我已收到此链接(http://cnfolio.com/public/libgd_drawing_reference.html),谢谢
答案 0 :(得分:1)
gdImagePolygon
具有以下签名:
gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)
gdImagePtr
是指向gdImage Structure
gdPointPtr
是指向gdPoint structure的指针(只有两个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);
答案 1 :(得分:0)
我从头开始编写此代码段,如果有任何错误,请通知我。感谢。
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 !
}
}
答案 2 :(得分:-1)
快速伪代码绘制N
边的多边形,边长为L
:
Procedure DrawPol (integer N,L)
Integer i
For i=1 To N
Draw (L)
Turn (360/N)
EndFor
EndProcedure
这个伪代码基于两个原语,这些原语在LOGO:
等语言中很常见Draw (L)
:在当前方向绘制一行L
像素Turn (A)
:通过向A
度添加Draw
度来更改当前方向
当前方向要使用Turn
功能实施Line
和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
EndProcedure
,您可以使用以下内容:
{{1}}