example of Bresenham's line algorithm
it depends on gtk+-2.0,using function gdk_draw_point to draw points.I wrote this little program because the stupid Chinese education:they are still writing c programs compiled by turbo c and with a "graphic.h" to draw graphics.
#include <gdk/gdk.h>//in fact,I do not know whether I need this
#include <gtk/gtk.h>
void linebres(int xa, int ya, int xb, int yb,GtkWidget *widget,GdkDrawable *drawable)
{
int dx = abs(xa-xb), dy = abs(ya-yb);
int p;
int twody, twodx;
int x,y,xend,yend;
float k=dy/dx;
int temp;
if(k > 1|| k < -1){
temp = xa;
xa = ya;
ya = temp;
temp = xb;
xb = yb;
yb = temp;
}
p = 2 * dy - dx;
twody = 2 * dy;
twodx = 2 * dx;
if(xa - xb > 0){
x = xb;
y = yb;
xend=xa;
}
else{
x = xa;
y = ya;
xend = xb;
}
gdk_draw_point (drawable,widget->style->black_gc,x,y);
while(x < xend){
x++;
if(p < 0)
p += twody;
else{
y++;
p += (twody-twodx);
}
gdk_draw_point (drawable,widget->style->black_gc,x,y);
}
}
static void on_destroy (GtkWidget * widget, gpointer data)
{
gtk_main_quit ();
}
gint expose_callback(GtkWidget *widget, GdkEventAny *event, gpointer data)
{
GdkDrawable *drawable;
int i;
drawable=widget->window;
linebres (10,5,180,60,widget,drawable);
return 0;
}
int main(int argc, char **argv)
{
gtk_init ( &argc, &argv);
GdkWindowAttr attr;
GtkWidget *window;
GtkWidget *widget = gtk_drawing_area_new ();
gtk_drawing_area_size (GTK_DRAWING_AREA (widget),200,200);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_add (GTK_CONTAINER (window),widget);
g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (on_destroy), NULL);
g_signal_connect (G_OBJECT(widget), "expose_event",GTK_SIGNAL_FUNC(expose_callback), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
1 comment:
Great work.
Post a Comment