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"
49 //##############################################################################################################
52 //# Authors: Martin Schaller (04/2008)
53 //##############################################################################################################
56 struct graphics_priv *priv;
57 struct graphics_methods meth;
58 struct graphics_font *font[16];
59 struct graphics_gc *gc[3];
63 //##############################################################################################################
66 //# Authors: Martin Schaller (04/2008)
67 //##############################################################################################################
71 //##############################################################################################################
72 //# Description: Creates a new graphics object
73 //# Comment: attr type required
74 //# Authors: Martin Schaller (04/2008)
75 //##############################################################################################################
76 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
78 struct graphics *this_;
79 struct attr *type_attr;
80 struct graphics_priv * (*graphicstype_new)(struct graphics_methods *meth, struct attr **attrs);
82 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
86 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
87 if (! graphicstype_new)
89 this_=g_new0(struct graphics, 1);
90 this_->priv=(*graphicstype_new)(&this_->meth, attrs);
91 this_->attrs=attr_list_dup(attrs);
95 //##############################################################################################################
98 //# Authors: Martin Schaller (04/2008)
99 //##############################################################################################################
100 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
102 return attr_generic_get_attr(this_->attrs, type, attr, iter);
105 //##############################################################################################################
108 //# Authors: Martin Schaller (04/2008)
109 //##############################################################################################################
110 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h)
112 struct graphics *this_;
113 this_=g_new0(struct graphics, 1);
114 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h);
118 //##############################################################################################################
121 //# Authors: Martin Schaller (04/2008)
122 //##############################################################################################################
123 void graphics_init(struct graphics *this_)
125 this_->gc[0]=graphics_gc_new(this_);
126 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
127 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
128 this_->gc[1]=graphics_gc_new(this_);
129 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000 });
130 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff });
131 this_->gc[2]=graphics_gc_new(this_);
132 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
133 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
134 this_->meth.background_gc(this_->priv, this_->gc[0]->priv);
137 //##############################################################################################################
140 //# Authors: Martin Schaller (04/2008)
141 //##############################################################################################################
142 void * graphics_get_data(struct graphics *this_, char *type)
144 return (this_->meth.get_data(this_->priv, type));
147 //##############################################################################################################
150 //# Authors: Martin Schaller (04/2008)
151 //##############################################################################################################
152 void graphics_register_resize_callback(struct graphics *this_, void (*callback)(void *data, int w, int h), void *data)
154 this_->meth.register_resize_callback(this_->priv, callback, data);
157 //##############################################################################################################
159 //# Comment: Called in navit.c
160 //# Authors: Martin Schaller (04/2008)
161 //##############################################################################################################
162 void graphics_register_button_callback(struct graphics *this_, void (*callback)(void *data, int pressed, int button, struct point *p), void *data)
164 this_->meth.register_button_callback(this_->priv, callback, data);
167 //##############################################################################################################
170 //# Authors: Martin Schaller (04/2008)
171 //##############################################################################################################
172 void graphics_register_motion_callback(struct graphics *this_, void (*callback)(void *data, struct point *p), void *data)
174 this_->meth.register_motion_callback(this_->priv, callback, data);
177 //##############################################################################################################
180 //# Authors: Martin Schaller (04/2008)
181 //##############################################################################################################
182 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
184 struct graphics_font *this_;
186 this_=g_new0(struct graphics_font,1);
187 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, size, flags);
191 //##############################################################################################################
194 //# Authors: Martin Schaller (04/2008)
195 //##############################################################################################################
196 struct graphics_gc * graphics_gc_new(struct graphics *gra)
198 struct graphics_gc *this_;
200 this_=g_new0(struct graphics_gc,1);
201 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
205 //##############################################################################################################
208 //# Authors: Martin Schaller (04/2008)
209 //##############################################################################################################
210 void graphics_gc_destroy(struct graphics_gc *gc)
212 gc->meth.gc_destroy(gc->priv);
216 //##############################################################################################################
219 //# Authors: Martin Schaller (04/2008)
220 //##############################################################################################################
221 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
223 gc->meth.gc_set_foreground(gc->priv, c);
226 //##############################################################################################################
229 //# Authors: Martin Schaller (04/2008)
230 //##############################################################################################################
231 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
233 gc->meth.gc_set_background(gc->priv, c);
236 //##############################################################################################################
239 //# Authors: Martin Schaller (04/2008)
240 //##############################################################################################################
241 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
243 gc->meth.gc_set_linewidth(gc->priv, width);
246 //##############################################################################################################
249 //# Authors: Martin Schaller (04/2008)
250 //##############################################################################################################
251 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
253 if (gc->meth.gc_set_dashes)
254 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
257 //##############################################################################################################
258 //# Description: Create a new image from file path scaled to w and h pixels
260 //# Authors: Martin Schaller (04/2008)
261 //##############################################################################################################
262 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
264 struct graphics_image *this_;
266 this_=g_new0(struct graphics_image,1);
269 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
277 //##############################################################################################################
278 //# Description: Create a new image from file path
280 //# Authors: Martin Schaller (04/2008)
281 //##############################################################################################################
282 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
284 return graphics_image_new_scaled(gra, path, -1, -1);
287 //##############################################################################################################
290 //# Authors: Martin Schaller (04/2008)
291 //##############################################################################################################
292 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
294 if (gra->meth.image_free)
295 gra->meth.image_free(gra->priv, img->priv);
299 //##############################################################################################################
302 //# Authors: Martin Schaller (04/2008)
303 //##############################################################################################################
304 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
306 this_->meth.draw_restore(this_->priv, p, w, h);
309 //##############################################################################################################
312 //# Authors: Martin Schaller (04/2008)
313 //##############################################################################################################
314 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
316 this_->meth.draw_mode(this_->priv, mode);
319 //##############################################################################################################
322 //# Authors: Martin Schaller (04/2008)
323 //##############################################################################################################
324 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
326 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
329 //##############################################################################################################
332 //# Authors: Martin Schaller (04/2008)
333 //##############################################################################################################
334 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
336 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
339 //##############################################################################################################
342 //# Authors: Martin Schaller (04/2008)
343 //##############################################################################################################
344 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
346 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
349 //##############################################################################################################
352 //# Authors: Martin Schaller (04/2008)
353 //##############################################################################################################
354 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)
356 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
359 //##############################################################################################################
362 //# Authors: Martin Schaller (04/2008)
363 //##############################################################################################################
364 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
366 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
369 //##############################################################################################################
372 //# Authors: Martin Schaller (04/2008)
373 //##############################################################################################################
374 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
376 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
385 //##############################################################################################################
388 //# Authors: Martin Schaller (04/2008)
389 //##############################################################################################################
390 static void popup_view_html(struct popup_item *item, char *file)
393 sprintf(command,"firefox %s", file);
397 //##############################################################################################################
400 //# Authors: Martin Schaller (04/2008)
401 //##############################################################################################################
402 static void graphics_popup(struct display_list *list, struct popup_item **popup)
408 struct popup_item *curr_item,*last=NULL;
410 mr=map_rect_new(item->map, NULL, NULL, 0);
411 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
412 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
414 if (item_attr_get(item, attr_name, &attr)) {
415 curr_item=popup_item_new_text(popup,attr.u.str,1);
416 if (item_attr_get(item, attr_info_html, &attr)) {
417 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
419 if (item_attr_get(item, attr_price_html, &attr)) {
420 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
422 curr_item->submenu=last;
425 map_rect_destroy(mr);
429 //##############################################################################################################
432 //# Authors: Martin Schaller (04/2008)
433 //##############################################################################################################
442 //##############################################################################################################
445 //# Authors: Martin Schaller (04/2008)
446 //##############################################################################################################
447 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
453 struct displayitem *di=l->data;
454 if (! di->displayed && di->item.type < type_line)
455 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
463 //##############################################################################################################
466 //# Authors: Martin Schaller (04/2008)
467 //##############################################################################################################
468 static void xdisplay_free(GHashTable *display_list)
470 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
473 //##############################################################################################################
476 //# Authors: Martin Schaller (04/2008)
477 //##############################################################################################################
478 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
480 struct displayitem *di;
485 len=sizeof(*di)+count*sizeof(*pnt);
487 len+=strlen(label)+1;
491 di=(struct displayitem *)p;
493 p+=sizeof(*di)+count*sizeof(*pnt);
497 strcpy(di->label, label);
501 memcpy(di->pnt, pnt, count*sizeof(*pnt));
503 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
504 l=g_list_prepend(l, di);
505 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
509 //##############################################################################################################
512 //# Authors: Martin Schaller (04/2008)
513 //##############################################################################################################
514 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)
520 tl=strlen(label)*400;
521 for (i = 0 ; i < count-1 ; i++) {
526 l=(int)sqrt((float)(dx*dx+dy*dy));
543 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
545 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
550 //##############################################################################################################
553 //# Authors: Martin Schaller (04/2008)
554 //##############################################################################################################
555 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
558 GList *l,*ls,*es,*types;
560 struct graphics_gc *gc = NULL;
561 struct graphics_image *img;
569 type=GPOINTER_TO_INT(types->data);
570 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
573 graphics_gc_destroy(gc);
577 struct displayitem *di;
581 gc=graphics_gc_new(gra);
582 gc->meth.gc_set_foreground(gc->priv, &e->color);
585 case element_polygon:
586 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
588 case element_polyline:
589 if (e->u.polyline.width > 1)
590 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
591 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
592 graphics_gc_set_dashes(gc, e->u.polyline.width, 0,
593 e->u.polyline.dash_table,
594 e->u.polyline.dash_num);
595 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
598 if (e->u.circle.width > 1)
599 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
600 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
601 if (di->label && e->label_size) {
604 if (! gra->font[e->label_size])
605 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
606 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);
611 if (! gra->font[e->label_size])
612 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
613 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
618 char *icon=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/xpm/", e->u.icon.src, NULL);
619 img=graphics_image_new(gra, icon);
622 g_warning("failed to load icon '%s'\n", e->u.icon.src);
625 p.x=di->pnt[0].x - img->hot.x;
626 p.y=di->pnt[0].y - img->hot.y;
627 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
628 graphics_image_free(gra, img);
633 dbg(1,"image: '%s'\n", di->label);
634 if (gra->meth.draw_image_warp)
635 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
637 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
640 printf("Unhandled element type %d\n", e->type);
645 types=g_list_next(types);
650 graphics_gc_destroy(gc);
653 //##############################################################################################################
656 //# Authors: Martin Schaller (04/2008)
657 //##############################################################################################################
658 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
661 struct itemtype *itm;
666 if (order >= itm->order_min && order <= itm->order_max)
667 xdisplay_draw_elements(gra, display_list, itm);
668 itms=g_list_next(itms);
672 //##############################################################################################################
675 //# Authors: Martin Schaller (04/2008)
676 //##############################################################################################################
677 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
685 xdisplay_draw_layer(display_list, gra, lay, order);
686 lays=g_list_next(lays);
690 //##############################################################################################################
693 //# Authors: Martin Schaller (04/2008)
694 //##############################################################################################################
695 extern void *route_selection;
697 //##############################################################################################################
700 //# Authors: Martin Schaller (04/2008)
701 //##############################################################################################################
702 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
707 int conv,count,max=16384;
708 struct point pnt[max];
709 struct coord ca[max];
711 struct map_selection *sel;
713 pro=map_projection(m);
714 conv=map_requires_conversion(m);
715 sel=transform_get_selection(t, pro, order);
717 mr=map_rect_new(m, route_selection);
719 mr=map_rect_new(m, sel);
721 map_selection_destroy(sel);
724 while ((item=map_rect_get_item(mr))) {
725 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
726 if (item->type >= type_line && count < 2) {
727 dbg(1,"poly from map has only %d points\n", count);
730 if (item->type < type_line) {
731 if (! map_selection_contains_point(sel, &ca[0])) {
732 dbg(1,"point not visible\n");
735 } else if (item->type < type_area) {
736 if (! map_selection_contains_polyline(sel, ca, count)) {
737 dbg(1,"polyline not visible\n");
741 if (! map_selection_contains_polygon(sel, ca, count)) {
742 dbg(1,"polygon not visible\n");
747 dbg(0,"point count overflow\n", count);
748 count=transform(t, pro, ca, pnt, count, 1);
749 if (item->type >= type_line && count < 2) {
750 dbg(1,"poly from transform has only %d points\n", count);
753 if (!item_attr_get(item, attr_label, &attr))
755 if (conv && attr.u.str && attr.u.str[0]) {
756 char *str=map_convert_string(m, attr.u.str);
757 display_add(displaylist, item, count, pnt, str);
758 map_convert_free(str);
760 display_add(displaylist, item, count, pnt, attr.u.str);
762 map_rect_destroy(mr);
763 map_selection_destroy(sel);
766 //##############################################################################################################
769 //# Authors: Martin Schaller (04/2008)
770 //##############################################################################################################
771 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
775 struct mapset_handle *h;
781 while ((m=mapset_next(h, 1))) {
782 do_draw_map(displaylist, t, m, order);
787 //##############################################################################################################
790 //# Authors: Martin Schaller (04/2008)
791 //##############################################################################################################
792 int graphics_ready(struct graphics *this_)
797 //##############################################################################################################
800 //# Authors: Martin Schaller (04/2008)
801 //##############################################################################################################
802 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l)
804 int order=transform_get_order(trans);
808 // FIXME find a better place to set the background color
809 graphics_gc_set_background(gra->gc[0], l->color);
810 graphics_gc_set_foreground(gra->gc[0], l->color);
811 gra->meth.background_gc(gra->priv, gra->gc[0]->priv);
812 gra->meth.draw_mode(gra->priv, draw_mode_begin);
813 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
814 xdisplay_draw(displaylist->dl, gra, l, order);
815 gra->meth.draw_mode(gra->priv, draw_mode_end);
818 //##############################################################################################################
821 //# Authors: Martin Schaller (04/2008)
822 //##############################################################################################################
823 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
825 struct displaylist_handle *dlh;
826 struct displayitem *di;
829 dlh=graphics_displaylist_open(displaylist);
830 while ((di=graphics_displaylist_next(dlh))) {
831 for (i = 0 ; i < di->count ; i++) {
836 graphics_displaylist_close(dlh);
839 //##############################################################################################################
842 //# Authors: Martin Schaller (04/2008)
843 //##############################################################################################################
844 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
846 int order=transform_get_order(trans);
851 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));
854 xdisplay_free(displaylist->dl);
855 dbg(1,"order=%d\n", order);
859 for (i = 0 ; i < data_window_type_end; i++) {
860 data_window_begin(co->data_window[i]);
864 do_draw(displaylist, trans, mapsets, order);
865 // profile(1,"do_draw");
866 graphics_displaylist_draw(gra, displaylist, trans, l);
867 profile(1,"xdisplay_draw");
871 for (i = 0 ; i < data_window_type_end; i++) {
872 data_window_end(co->data_window[i]);
878 //##############################################################################################################
881 //# Authors: Martin Schaller (04/2008)
882 //##############################################################################################################
883 struct displaylist_handle {
884 GList *hl_head,*hl,*l;
887 //##############################################################################################################
890 //# Authors: Martin Schaller (04/2008)
891 //##############################################################################################################
892 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
894 struct displaylist_handle *ret;
896 ret=g_new0(struct displaylist_handle, 1);
897 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
902 //##############################################################################################################
905 //# Authors: Martin Schaller (04/2008)
906 //##############################################################################################################
907 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
909 struct displayitem *ret;
913 dlh->l=dlh->hl->data;
914 dlh->hl=g_list_next(dlh->hl);
917 dlh->l=g_list_next(dlh->l);
921 //##############################################################################################################
924 //# Authors: Martin Schaller (04/2008)
925 //##############################################################################################################
926 void graphics_displaylist_close(struct displaylist_handle *dlh)
928 g_list_free(dlh->hl_head);
932 //##############################################################################################################
935 //# Authors: Martin Schaller (04/2008)
936 //##############################################################################################################
937 struct displaylist * graphics_displaylist_new(void)
939 struct displaylist *ret=g_new(struct displaylist, 1);
941 ret->dl=g_hash_table_new(NULL,NULL);
946 //##############################################################################################################
949 //# Authors: Martin Schaller (04/2008)
950 //##############################################################################################################
951 struct item * graphics_displayitem_get_item(struct displayitem *di)
956 //##############################################################################################################
959 //# Authors: Martin Schaller (04/2008)
960 //##############################################################################################################
961 char * graphics_displayitem_get_label(struct displayitem *di)
966 //##############################################################################################################
969 //# Authors: Martin Schaller (04/2008)
970 //##############################################################################################################
971 static int within_dist_point(struct point *p0, struct point *p1, int dist)
973 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
975 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
977 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
983 //##############################################################################################################
986 //# Authors: Martin Schaller (04/2008)
987 //##############################################################################################################
988 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
994 vx=line_p1->x-line_p0->x;
995 vy=line_p1->y-line_p0->y;
1001 return within_dist_point(p, line_p0, dist);
1004 return within_dist_point(p, line_p1, dist);
1006 line_p.x=line_p0->x+vx*c1/c2;
1007 line_p.y=line_p0->y+vy*c1/c2;
1008 return within_dist_point(p, &line_p, dist);
1011 //##############################################################################################################
1014 //# Authors: Martin Schaller (04/2008)
1015 //##############################################################################################################
1016 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1019 for (i = 0 ; i < count-1 ; i++) {
1020 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1025 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1029 //##############################################################################################################
1032 //# Authors: Martin Schaller (04/2008)
1033 //##############################################################################################################
1034 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1037 for (i = 0, j = count-1; i < count; j = i++) {
1038 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1039 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1040 (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))
1044 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1048 //##############################################################################################################
1051 //# Authors: Martin Schaller (04/2008)
1052 //##############################################################################################################
1053 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1055 if (di->item.type < type_line) {
1056 return within_dist_point(p, &di->pnt[0], dist);
1058 if (di->item.type < type_area) {
1059 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1061 return within_dist_polygon(p, di->pnt, di->count, dist);