2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 //##############################################################################################################
25 //# Authors: Martin Schaller (04/2008)
27 //##############################################################################################################
35 #include "draw_info.h"
38 #include "projection.h"
41 #include "transform.h"
50 static char *navit_sharedir;
52 //##############################################################################################################
55 //# Authors: Martin Schaller (04/2008)
56 //##############################################################################################################
59 struct graphics_priv *priv;
60 struct graphics_methods meth;
62 struct graphics_font *font[16];
63 struct graphics_gc *gc[3];
65 struct callback_list *cbl;
75 * Creates a new graphics object
79 * @author Martin Schaller (04/2008)
81 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
83 struct graphics *this_;
84 struct attr *type_attr;
85 struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs);
87 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
91 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
92 if (! graphicstype_new)
94 this_=g_new0(struct graphics, 1);
95 this_->cbl=callback_list_new();
96 this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs);
97 this_->attrs=attr_list_dup(attrs);
105 * @author Martin Schaller (04/2008)
107 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
109 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
116 * @author Martin Schaller (04/2008)
118 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h)
120 struct graphics *this_;
121 this_=g_new0(struct graphics, 1);
122 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h);
130 * @author Martin Schaller (04/2008)
132 void graphics_init(struct graphics *this_)
134 this_->gc[0]=graphics_gc_new(this_);
135 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
136 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
137 this_->gc[1]=graphics_gc_new(this_);
138 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
139 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
140 this_->gc[2]=graphics_gc_new(this_);
141 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
142 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
143 this_->meth.background_gc(this_->priv, this_->gc[0]->priv);
144 navit_sharedir = getenv("NAVIT_SHAREDIR");
151 * @author Martin Schaller (04/2008)
153 void * graphics_get_data(struct graphics *this_, char *type)
155 return (this_->meth.get_data(this_->priv, type));
162 * @author Martin Schaller (04/2008)
164 void graphics_register_resize_callback(struct graphics *this_, void (*callback)(void *data, int w, int h), void *data)
166 this_->meth.register_resize_callback(this_->priv, callback, data);
173 * @author Martin Schaller (04/2008)
175 void graphics_register_button_callback(struct graphics *this_, void (*callback)(void *data, int pressed, int button, struct point *p), void *data)
177 this_->meth.register_button_callback(this_->priv, callback, data);
184 * @author Martin Schaller (04/2008)
186 void graphics_register_motion_callback(struct graphics *this_, void (*callback)(void *data, struct point *p), void *data)
188 this_->meth.register_motion_callback(this_->priv, callback, data);
195 * @author Martin Schaller (04/2008)
197 void graphics_register_keypress_callback(struct graphics *this_, void (*callback)(void *data, char *key), void *data)
199 this_->meth.register_keypress_callback(this_->priv, callback, data);
202 void graphics_add_callback(struct graphics *this_, struct callback *cb)
204 callback_list_add(this_->cbl, cb);
211 * @author Martin Schaller (04/2008)
213 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
215 struct graphics_font *this_;
217 this_=g_new0(struct graphics_font,1);
218 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
223 * Free all loaded fonts.
224 * Used when switching layouts.
225 * @param gra The graphics instance
227 * @author Sarah Nordstrom (05/2008)
229 void graphics_font_destroy_all(struct graphics *gra)
232 for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) {
233 if(!gra->font[i]) continue;
234 gra->font[i]->meth.font_destroy(gra->font[i]->priv);
243 * @author Martin Schaller (04/2008)
245 struct graphics_gc * graphics_gc_new(struct graphics *gra)
247 struct graphics_gc *this_;
249 this_=g_new0(struct graphics_gc,1);
250 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
258 * @author Martin Schaller (04/2008)
260 void graphics_gc_destroy(struct graphics_gc *gc)
262 gc->meth.gc_destroy(gc->priv);
270 * @author Martin Schaller (04/2008)
272 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
274 gc->meth.gc_set_foreground(gc->priv, c);
281 * @author Martin Schaller (04/2008)
283 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
285 gc->meth.gc_set_background(gc->priv, c);
292 * @author Martin Schaller (04/2008)
294 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
296 gc->meth.gc_set_linewidth(gc->priv, width);
303 * @author Martin Schaller (04/2008)
305 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
307 if (gc->meth.gc_set_dashes)
308 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
312 * Create a new image from file path scaled to w and h pixels
313 * @param gra the graphics instance
314 * @param path path of the image to load
315 * @param w width to rescale to
316 * @param h height to rescale to
318 * @author Martin Schaller (04/2008)
320 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
322 struct graphics_image *this_;
324 this_=g_new0(struct graphics_image,1);
327 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
336 * Create a new image from file path
337 * @param gra the graphics instance
338 * @param path path of the image to load
340 * @author Martin Schaller (04/2008)
342 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
344 return graphics_image_new_scaled(gra, path, -1, -1);
351 * @author Martin Schaller (04/2008)
353 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
355 if (gra->meth.image_free)
356 gra->meth.image_free(gra->priv, img->priv);
364 * @author Martin Schaller (04/2008)
366 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
368 this_->meth.draw_restore(this_->priv, p, w, h);
375 * @author Martin Schaller (04/2008)
377 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
379 this_->meth.draw_mode(this_->priv, mode);
386 * @author Martin Schaller (04/2008)
388 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
390 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
397 * @author Martin Schaller (04/2008)
399 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
401 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
408 * @author Martin Schaller (04/2008)
410 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
412 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
419 * @author Martin Schaller (04/2008)
421 void graphics_draw_text(struct graphics *this_, struct graphics_gc *gc1, struct graphics_gc *gc2, struct graphics_font *font, char *text, struct point *p, int dx, int dy)
423 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
430 * @author Martin Schaller (04/2008)
432 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
434 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
441 * @author Martin Schaller (04/2008)
443 void graphics_overlay_disable(struct graphics *this_, int disable)
445 if (this_->meth.overlay_disable)
446 this_->meth.overlay_disable(this_->priv, disable);
453 * @author Martin Schaller (04/2008)
455 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
457 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
466 //##############################################################################################################
469 //# Authors: Martin Schaller (04/2008)
470 //##############################################################################################################
471 static void popup_view_html(struct popup_item *item, char *file)
474 sprintf(command,"firefox %s", file);
478 //##############################################################################################################
481 //# Authors: Martin Schaller (04/2008)
482 //##############################################################################################################
483 static void graphics_popup(struct display_list *list, struct popup_item **popup)
489 struct popup_item *curr_item,*last=NULL;
491 mr=map_rect_new(item->map, NULL, NULL, 0);
492 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
493 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
495 if (item_attr_get(item, attr_name, &attr)) {
496 curr_item=popup_item_new_text(popup,attr.u.str,1);
497 if (item_attr_get(item, attr_info_html, &attr)) {
498 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
500 if (item_attr_get(item, attr_price_html, &attr)) {
501 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
503 curr_item->submenu=last;
506 map_rect_destroy(mr);
514 * @author Martin Schaller (04/2008)
528 * @author Martin Schaller (04/2008)
530 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
536 struct displayitem *di=l->data;
537 if (! di->displayed && di->item.type < type_line)
538 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
550 * @author Martin Schaller (04/2008)
552 static void xdisplay_free(GHashTable *display_list)
554 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
561 * @author Martin Schaller (04/2008)
563 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
565 struct displayitem *di;
570 len=sizeof(*di)+count*sizeof(*pnt);
572 len+=strlen(label)+1;
576 di=(struct displayitem *)p;
578 p+=sizeof(*di)+count*sizeof(*pnt);
582 strcpy(di->label, label);
586 memcpy(di->pnt, pnt, count*sizeof(*pnt));
588 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
589 l=g_list_prepend(l, di);
590 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
598 * @author Martin Schaller (04/2008)
600 static void label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
606 tl=strlen(label)*400;
607 for (i = 0 ; i < count-1 ; i++) {
612 l=(int)sqrt((float)(dx*dx+dy*dy));
629 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
631 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
636 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
639 pnt[0]=pnt[1]=pnt[2]=*p;
640 pnt[0].x+=-dx*l/65536+dy*l/65536;
641 pnt[0].y+=-dy*l/65536-dx*l/65536;
642 pnt[2].x+=-dx*l/65536-dy*l/65536;
643 pnt[2].y+=-dy*l/65536+dx*l/65536;
644 gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
647 static void display_draw_arrows(struct displayitem *di, struct graphics_gc *gc, struct graphics *gra)
651 for (i = 0 ; i < di->count-1 ; i++) {
652 dx=di->pnt[i+1].x-di->pnt[i].x;
653 dy=di->pnt[i+1].y-di->pnt[i].y;
661 display_draw_arrow(&p, dx, dy, 10, gc, gra);
665 display_draw_arrow(&p, dx, dy, 10, gc, gra);
674 * @author Martin Schaller (04/2008)
676 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
679 GList *l,*ls,*es,*types;
681 struct graphics_gc *gc = NULL;
682 struct graphics_image *img;
691 type=GPOINTER_TO_INT(types->data);
692 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
695 graphics_gc_destroy(gc);
699 struct displayitem *di;
703 gc=graphics_gc_new(gra);
704 gc->meth.gc_set_foreground(gc->priv, &e->color);
707 case element_polygon:
708 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
710 case element_polyline:
711 if (e->u.polyline.width > 1)
712 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
713 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
714 graphics_gc_set_dashes(gc, e->u.polyline.width,
715 e->u.polyline.dash_offset,
716 e->u.polyline.dash_table,
717 e->u.polyline.dash_num);
718 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
721 if (e->u.circle.width > 1)
722 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
723 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
724 if (di->label && e->label_size) {
727 if (! gra->font[e->label_size])
728 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
729 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->label_size]->priv, di->label, &p, 0x10000, 0);
734 if (! gra->font[e->label_size])
735 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
736 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
741 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
742 img=graphics_image_new(gra, path);
744 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
747 p.x=di->pnt[0].x - img->hot.x;
748 p.y=di->pnt[0].y - img->hot.y;
749 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
750 graphics_image_free(gra, img);
755 dbg(1,"image: '%s'\n", di->label);
756 if (gra->meth.draw_image_warp)
757 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
759 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
762 display_draw_arrows(di,gc,gra);
765 printf("Unhandled element type %d\n", e->type);
770 types=g_list_next(types);
775 graphics_gc_destroy(gc);
782 * @author Martin Schaller (04/2008)
784 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
787 struct itemtype *itm;
792 if (order >= itm->order_min && order <= itm->order_max)
793 xdisplay_draw_elements(gra, display_list, itm);
794 itms=g_list_next(itms);
802 * @author Martin Schaller (04/2008)
804 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
812 xdisplay_draw_layer(display_list, gra, lay, order);
813 lays=g_list_next(lays);
821 * @author Martin Schaller (04/2008)
823 extern void *route_selection;
829 * @author Martin Schaller (04/2008)
831 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
836 int conv,count,max=16384;
837 struct point pnt[max];
838 struct coord ca[max];
840 struct map_selection *sel;
842 pro=map_projection(m);
843 conv=map_requires_conversion(m);
844 sel=transform_get_selection(t, pro, order);
846 mr=map_rect_new(m, route_selection);
848 mr=map_rect_new(m, sel);
850 map_selection_destroy(sel);
853 while ((item=map_rect_get_item(mr))) {
854 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
855 if (item->type >= type_line && count < 2) {
856 dbg(1,"poly from map has only %d points\n", count);
859 if (item->type < type_line) {
860 if (! map_selection_contains_point(sel, &ca[0])) {
861 dbg(1,"point not visible\n");
864 } else if (item->type < type_area) {
865 if (! map_selection_contains_polyline(sel, ca, count)) {
866 dbg(1,"polyline not visible\n");
870 if (! map_selection_contains_polygon(sel, ca, count)) {
871 dbg(1,"polygon not visible\n");
876 dbg(0,"point count overflow\n", count);
877 count=transform(t, pro, ca, pnt, count, 1);
878 if (item->type >= type_line && count < 2) {
879 dbg(1,"poly from transform has only %d points\n", count);
882 if (!item_attr_get(item, attr_label, &attr))
884 if (conv && attr.u.str && attr.u.str[0]) {
885 char *str=map_convert_string(m, attr.u.str);
886 display_add(displaylist, item, count, pnt, str);
887 map_convert_free(str);
889 display_add(displaylist, item, count, pnt, attr.u.str);
891 map_rect_destroy(mr);
892 map_selection_destroy(sel);
899 * @author Martin Schaller (04/2008)
901 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
905 struct mapset_handle *h;
911 while ((m=mapset_next(h, 1))) {
912 do_draw_map(displaylist, t, m, order);
921 * @author Martin Schaller (04/2008)
923 int graphics_ready(struct graphics *this_)
932 * @author Martin Schaller (04/2008)
934 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
936 int order=transform_get_order(trans);
940 // FIXME find a better place to set the background color
941 graphics_gc_set_background(gra->gc[0], &l->color);
942 graphics_gc_set_foreground(gra->gc[0], &l->color);
943 gra->default_font = g_strdup(l->font);
944 gra->meth.background_gc(gra->priv, gra->gc[0]->priv);
945 gra->meth.draw_mode(gra->priv, draw_mode_begin);
946 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
947 xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
949 callback_list_call_attr_0(gra->cbl, attr_postdraw);
950 gra->meth.draw_mode(gra->priv, draw_mode_end);
957 * @author Martin Schaller (04/2008)
959 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
961 struct displaylist_handle *dlh;
962 struct displayitem *di;
965 dlh=graphics_displaylist_open(displaylist);
966 while ((di=graphics_displaylist_next(dlh))) {
967 for (i = 0 ; i < di->count ; i++) {
972 graphics_displaylist_close(dlh);
979 * @author Martin Schaller (04/2008)
981 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
983 int order=transform_get_order(trans);
988 printf("scale=%d center=0x%x,0x%x mercator scale=%f\n", scale, co->trans->center.x, co->trans->center.y, transform_scale(co->trans->center.y));
991 xdisplay_free(displaylist->dl);
992 dbg(1,"order=%d\n", order);
996 for (i = 0 ; i < data_window_type_end; i++) {
997 data_window_begin(co->data_window[i]);
1001 order+=l->order_delta;
1002 do_draw(displaylist, trans, mapsets, order);
1003 // profile(1,"do_draw");
1004 graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1005 profile(1,"xdisplay_draw");
1009 for (i = 0 ; i < data_window_type_end; i++) {
1010 data_window_end(co->data_window[i]);
1020 * @author Martin Schaller (04/2008)
1022 struct displaylist_handle {
1023 GList *hl_head,*hl,*l;
1030 * @author Martin Schaller (04/2008)
1032 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1034 struct displaylist_handle *ret;
1036 ret=g_new0(struct displaylist_handle, 1);
1037 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1046 * @author Martin Schaller (04/2008)
1048 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1050 struct displayitem *ret;
1054 dlh->l=dlh->hl->data;
1055 dlh->hl=g_list_next(dlh->hl);
1058 dlh->l=g_list_next(dlh->l);
1066 * @author Martin Schaller (04/2008)
1068 void graphics_displaylist_close(struct displaylist_handle *dlh)
1070 g_list_free(dlh->hl_head);
1078 * @author Martin Schaller (04/2008)
1080 struct displaylist * graphics_displaylist_new(void)
1082 struct displaylist *ret=g_new(struct displaylist, 1);
1084 ret->dl=g_hash_table_new(NULL,NULL);
1093 * @author Martin Schaller (04/2008)
1095 struct item * graphics_displayitem_get_item(struct displayitem *di)
1104 * @author Martin Schaller (04/2008)
1106 char * graphics_displayitem_get_label(struct displayitem *di)
1115 * @author Martin Schaller (04/2008)
1117 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1119 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1121 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1123 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1133 * @author Martin Schaller (04/2008)
1135 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1139 struct point line_p;
1141 vx=line_p1->x-line_p0->x;
1142 vy=line_p1->y-line_p0->y;
1148 return within_dist_point(p, line_p0, dist);
1151 return within_dist_point(p, line_p1, dist);
1153 line_p.x=line_p0->x+vx*c1/c2;
1154 line_p.y=line_p0->y+vy*c1/c2;
1155 return within_dist_point(p, &line_p, dist);
1162 * @author Martin Schaller (04/2008)
1164 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1167 for (i = 0 ; i < count-1 ; i++) {
1168 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1173 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1181 * @author Martin Schaller (04/2008)
1183 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1186 for (i = 0, j = count-1; i < count; j = i++) {
1187 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1188 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1189 (p->x < (poly_pnt[j].x - poly_pnt[i].x) * (p->y - poly_pnt[i].y) / (poly_pnt[j].y - poly_pnt[i].y) + poly_pnt[i].x))
1193 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1201 * @author Martin Schaller (04/2008)
1203 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1205 if (di->item.type < type_line) {
1206 return within_dist_point(p, &di->pnt[0], dist);
1208 if (di->item.type < type_area) {
1209 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1211 return within_dist_polygon(p, di->pnt, di->count, dist);