gview.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. #include <cursor.h>
  7. #include <stdio.h>
  8. #define Never 0xffffffff /* Maximum ulong */
  9. #define LOG2 0.301029995664
  10. #define Button_bit(b) (1 << ((b)-1))
  11. enum {
  12. But1 = Button_bit(1),/* mouse buttons for events */
  13. But2 = Button_bit(2),
  14. But3 = Button_bit(3),
  15. };
  16. int cantmv = 1; /* disallow rotate and move? 0..1 */
  17. int top_border, bot_border, lft_border, rt_border;
  18. int lft_border0; /* lft_border for y-axis labels >0 */
  19. int top_left, top_right; /* edges of top line free space */
  20. int Mv_delay = 400; /* msec for button click vs. button hold down */
  21. int Dotrad = 2; /* dot radius in pixels */
  22. int framewd=1; /* line thickness for frame (pixels) */
  23. int framesep=1; /* distance between frame and surrounding text */
  24. int outersep=1; /* distance: surrounding text to screen edge */
  25. Point sdigit; /* size of a digit in the font */
  26. Point smaxch; /* assume any character in font fits in this */
  27. double underscan = .05; /* fraction of frame initially unused per side */
  28. double fuzz = 6; /* selection tolerance in pixels */
  29. int tick_len = 15; /* length of axis label tick mark in pixels */
  30. FILE* logfil = 0; /* dump selected points here if nonzero */
  31. #define labdigs 3 /* allow this many sig digits in axis labels */
  32. #define digs10pow 1000 /* pow(10,labdigs) */
  33. #define axis_color clr_im(DLtblue)
  34. /********************************* Utilities *********************************/
  35. /* Prepend string s to null-terminated string in n-byte buffer buf[], truncating if
  36. necessary and using a space to separate s from the rest of buf[].
  37. */
  38. char* str_insert(char* buf, char* s, int n)
  39. {
  40. int blen, slen = strlen(s) + 1;
  41. if (slen >= n)
  42. {strncpy(buf,s,n); buf[n-1]='\0'; return buf;}
  43. blen = strlen(buf);
  44. if (blen >= n-slen)
  45. buf[blen=n-slen-1] = '\0';
  46. memmove(buf+slen, buf, slen+blen+1);
  47. memcpy(buf, s, slen-1);
  48. buf[slen-1] = ' ';
  49. return buf;
  50. }
  51. /* Alter string smain (without lengthening it) so as to remove the first occurrence of
  52. ssub, assuming ssub is ASCII. Return nonzero (true) if string smain had to be changed.
  53. In spite of the ASCII-centric appearance, I think this can handle UTF in smain.
  54. */
  55. int remove_substr(char* smain, char* ssub)
  56. {
  57. char *ss, *s = strstr(smain, ssub);
  58. int n = strlen(ssub);
  59. if (s==0)
  60. return 0;
  61. if (islower(s[n]))
  62. s[0] ^= 32; /* probably tolower(s[0]) or toupper(s[0]) */
  63. else {
  64. for (ss=s+n; *ss!=0; s++, ss++)
  65. *s = *ss;
  66. *s = '\0';
  67. }
  68. return 1;
  69. }
  70. void adjust_border(Font* f)
  71. {
  72. int sep = framesep + outersep;
  73. sdigit = stringsize(f, "8");
  74. smaxch = stringsize(f, "MMMg");
  75. smaxch.x = (smaxch.x + 3)/4;
  76. lft_border0 = (1+labdigs)*sdigit.x + framewd + sep;
  77. rt_border = (lft_border0 - sep)/2 + outersep;
  78. bot_border = sdigit.y + framewd + sep;
  79. top_border = smaxch.y + framewd + sep;
  80. lft_border = lft_border0; /* this gets reset later */
  81. }
  82. int is_off_screen(Point p)
  83. {
  84. const Rectangle* r = &(screen->r);
  85. return p.x-r->min.x<lft_border || r->max.x-p.x<rt_border
  86. || p.y-r->min.y<=top_border || r->max.y-p.y<=bot_border;
  87. }
  88. Cursor bullseye =
  89. {
  90. {-7, -7},
  91. {
  92. 0x1F, 0xF8, 0x3F, 0xFC, 0x7F, 0xFE, 0xFB, 0xDF,
  93. 0xF3, 0xCF, 0xE3, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF,
  94. 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0xC7, 0xF3, 0xCF,
  95. 0x7B, 0xDF, 0x7F, 0xFE, 0x3F, 0xFC, 0x1F, 0xF8,
  96. },
  97. {
  98. 0x00, 0x00, 0x0F, 0xF0, 0x31, 0x8C, 0x21, 0x84,
  99. 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x7F, 0xFE,
  100. 0x7F, 0xFE, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
  101. 0x21, 0x84, 0x31, 0x8C, 0x0F, 0xF0, 0x00, 0x00,
  102. }
  103. };
  104. int get_1click(int but, Mouse* m, Cursor* curs)
  105. {
  106. if (curs)
  107. esetcursor(curs);
  108. while (m->buttons==0)
  109. *m = emouse();
  110. if (curs)
  111. esetcursor(0);
  112. return (m->buttons==Button_bit(but));
  113. }
  114. /* Wait until but goes up or until a mouse event's msec passes tlimit.
  115. Return a boolean result that tells whether the button went up.
  116. */
  117. int lift_button(int but, Mouse* m, int tlimit)
  118. {
  119. do { *m = emouse();
  120. if (m->msec >= tlimit)
  121. return 0;
  122. } while (m->buttons & Button_bit(but));
  123. return 1;
  124. }
  125. /* Set *m to the last pending mouse event, or the first one where but is up.
  126. If no mouse events are pending, wait for the next one.
  127. */
  128. void latest_mouse(int but, Mouse* m)
  129. {
  130. int bbit = Button_bit(but);
  131. do { *m = emouse();
  132. } while ((m->buttons & bbit) && ecanmouse());
  133. }
  134. /*********************************** Colors ***********************************/
  135. enum { DOrange=0xffaa00FF, Dgray=0xbbbbbbFF, DDkgreen=0x009900FF,
  136. DDkred=0xcc0000FF, DViolet=0x990099FF, DDkyellow=0xaaaa00FF,
  137. DLtblue=0xaaaaffFF, DPink=0xffaaaaFF,
  138. /* ndraw.h sets DBlack, DBlue, DRed, DYellow, DGreen,
  139. DCyan, DMagenta, DWhite */
  140. };
  141. typedef struct color_ref {
  142. ulong c; /* RGBA pixel color */
  143. char* nam; /* ASCII name (matched to input, used in output)*/
  144. Image* im; /* replicated solid-color image */
  145. } color_ref;
  146. color_ref clrtab[] = {
  147. DRed, "Red", 0,
  148. DPink, "Pink", 0,
  149. DDkred, "Dkred", 0,
  150. DOrange, "Orange", 0,
  151. DYellow, "Yellow", 0,
  152. DDkyellow, "Dkyellow", 0,
  153. DGreen, "Green", 0,
  154. DDkgreen, "Dkgreen", 0,
  155. DCyan, "Cyan", 0,
  156. DBlue, "Blue", 0,
  157. DLtblue, "Ltblue", 0,
  158. DMagenta, "Magenta", 0,
  159. DViolet, "Violet", 0,
  160. Dgray, "Gray", 0,
  161. DBlack, "Black", 0,
  162. DWhite, "White", 0,
  163. DNofill, 0, 0 /* DNofill means "end of data" */
  164. };
  165. void init_clrtab(void)
  166. {
  167. int i;
  168. Rectangle r = Rect(0,0,1,1);
  169. for (i=0; clrtab[i].c!=DNofill; i++)
  170. clrtab[i].im = allocimage(display, r, CMAP8, 1, clrtab[i].c);
  171. /* should check for 0 result? */
  172. }
  173. int clrim_id(Image* clr)
  174. {
  175. int i;
  176. for (i=0; clrtab[i].im!=clr; i++)
  177. if (clrtab[i].c==DNofill)
  178. exits("bad image color");
  179. return i;
  180. }
  181. int clr_id(int clr)
  182. {
  183. int i;
  184. for (i=0; clrtab[i].c!=clr; i++)
  185. if (clrtab[i].c==DNofill)
  186. exits("bad color");
  187. return i;
  188. }
  189. #define clr_im(clr) clrtab[clr_id(clr)].im
  190. /* This decides what color to use for a polyline based on the label it has in the
  191. input file. Whichever color name comes first is the winner, otherwise return black.
  192. */
  193. Image* nam2clr(const char* nam, int *idxdest)
  194. {
  195. char *c, *cbest=nam;
  196. int i, ibest=-1;
  197. if (*nam!=0)
  198. for (i=0; clrtab[i].nam!=0; i++) {
  199. c = strstr(nam,clrtab[i].nam);
  200. if (c!=0 && (ibest<0 || c<cbest))
  201. {ibest=i; cbest=c;}
  202. }
  203. if (idxdest!=0)
  204. *idxdest = (ibest<0) ? clr_id(DBlack) : ibest;
  205. return (ibest<0) ? clr_im(DBlack) : clrtab[ibest].im;
  206. }
  207. /* A polyline is initial drawn in thick mode iff its label in the file contains "Thick" */
  208. int nam2thick(const char* nam)
  209. {
  210. return strstr(nam,"Thick")==0 ? 0 : 1;
  211. }
  212. /* Alter string nam so that nam2thick() and nam2clr() agree with th and clr, using
  213. buf[] (a buffer of length bufn) to store the result if it differs from nam.
  214. We go to great pains to perform this alteration in a manner that will seem natural
  215. to the user, i.e., we try removing a suitably isolated color name before inserting
  216. a new one.
  217. */
  218. char* nam_with_thclr(char* nam, int th, Image* clr, char* buf, int bufn)
  219. {
  220. int clr0i, th0=nam2thick(nam);
  221. Image* clr0 = nam2clr(nam, &clr0i);
  222. char *clr0s;
  223. if (th0==th && clr0==clr)
  224. return nam;
  225. clr0s = clrtab[clr0i].nam;
  226. if (strlen(nam)<bufn) strcpy(buf,nam);
  227. else {strncpy(buf,nam,bufn); buf[bufn-1]='\0';}
  228. if (clr0 != clr)
  229. remove_substr(buf, clr0s);
  230. if (th0 > th)
  231. while (remove_substr(buf, "Thick"))
  232. /* do nothing */;
  233. if (nam2clr(buf,0) != clr)
  234. str_insert(buf, clrtab[clrim_id(clr)].nam, bufn);
  235. if (th0 < th)
  236. str_insert(buf, "Thick", bufn);
  237. return buf;
  238. }
  239. /****************************** Data structures ******************************/
  240. Image* mv_bkgd; /* Background image (usually 0) */
  241. typedef struct fpoint {
  242. double x, y;
  243. } fpoint;
  244. typedef struct frectangle {
  245. fpoint min, max;
  246. } frectangle;
  247. frectangle empty_frect = {1e30, 1e30, -1e30, -1e30};
  248. /* When *r2 is transformed by y=y-x*slant, might it intersect *r1 ?
  249. */
  250. int fintersects(const frectangle* r1, const frectangle* r2, double slant)
  251. {
  252. double x2min=r2->min.x, x2max=r2->max.x;
  253. if (r1->max.x <= x2min || x2max <= r1->min.x)
  254. return 0;
  255. if (slant >=0)
  256. {x2min*=slant; x2max*=slant;}
  257. else {double t=x2min*slant; x2min=x2max*slant; x2max=t;}
  258. return r1->max.y > r2->min.y-x2max && r2->max.y-x2min > r1->min.y;
  259. }
  260. int fcontains(const frectangle* r, fpoint p)
  261. {
  262. return r->min.x <=p.x && p.x<= r->max.x && r->min.y <=p.y && p.y<= r->max.y;
  263. }
  264. void grow_bb(frectangle* dest, const frectangle* r)
  265. {
  266. if (r->min.x < dest->min.x) dest->min.x=r->min.x;
  267. if (r->min.y < dest->min.y) dest->min.y=r->min.y;
  268. if (r->max.x > dest->max.x) dest->max.x=r->max.x;
  269. if (r->max.y > dest->max.y) dest->max.y=r->max.y;
  270. }
  271. void slant_frect(frectangle *r, double sl)
  272. {
  273. r->min.y += sl*r->min.x;
  274. r->max.y += sl*r->max.x;
  275. }
  276. fpoint fcenter(const frectangle* r)
  277. {
  278. fpoint c;
  279. c.x = .5*(r->max.x + r->min.x);
  280. c.y = .5*(r->max.y + r->min.y);
  281. return c;
  282. }
  283. typedef struct fpolygon {
  284. fpoint* p; /* a malloc'ed array */
  285. int n; /* p[] has n elements: p[0..n] */
  286. frectangle bb; /* bounding box */
  287. char* nam; /* name of this polygon (malloc'ed) */
  288. int thick; /* use 1+2*thick pixel wide lines */
  289. Image* clr; /* Color to use when drawing this */
  290. struct fpolygon* link;
  291. } fpolygon;
  292. typedef struct fpolygons {
  293. fpolygon* p; /* the head of a linked list */
  294. frectangle bb; /* overall bounding box */
  295. frectangle disp; /* part being mapped onto screen->r */
  296. double slant_ht; /* controls how disp is slanted */
  297. } fpolygons;
  298. fpolygons univ = { /* everything there is to display */
  299. 0,
  300. 1e30, 1e30, -1e30, -1e30,
  301. 0, 0, 0, 0,
  302. 2*1e30
  303. };
  304. void set_default_clrs(fpolygons* fps, fpolygon* fpstop)
  305. {
  306. fpolygon* fp;
  307. for (fp=fps->p; fp!=0 && fp!=fpstop; fp=fp->link) {
  308. fp->clr = nam2clr(fp->nam,0);
  309. fp->thick = nam2thick(fp->nam);
  310. }
  311. }
  312. void fps_invert(fpolygons* fps)
  313. {
  314. fpolygon *p, *r=0;
  315. for (p=fps->p; p!=0;) {
  316. fpolygon* q = p;
  317. p = p->link;
  318. q->link = r;
  319. r = q;
  320. }
  321. fps->p = r;
  322. }
  323. void fp_remove(fpolygons* fps, fpolygon* fp)
  324. {
  325. fpolygon *q, **p = &fps->p;
  326. while (*p!=fp)
  327. if (*p==0)
  328. return;
  329. else p = &(*p)->link;
  330. *p = fp->link;
  331. fps->bb = empty_frect;
  332. for (q=fps->p; q!=0; q=q->link)
  333. grow_bb(&fps->bb, &q->bb);
  334. }
  335. /* The transform maps abstract fpoint coordinates (the ones used in the input)
  336. to the current screen coordinates. The do_untransform() macros reverses this.
  337. If univ.slant_ht is not the height of univ.disp, the actual region in the
  338. abstract coordinates is a parallelogram inscribed in univ.disp with two
  339. vertical edges and two slanted slanted edges: slant_ht>0 means that the
  340. vertical edges have height slant_ht and the parallelogram touches the lower
  341. left and upper right corners of univ.disp; slant_ht<0 refers to a parallelogram
  342. of height -slant_ht that touches the other two corners of univ.disp.
  343. NOTE: the ytransform macro assumes that tr->sl times the x coordinate has
  344. already been subtracted from yy.
  345. */
  346. typedef struct transform {
  347. double sl;
  348. fpoint o, sc; /* (x,y):->(o.x+sc.x*x, o.y+sc.y*y+sl*x) */
  349. } transform;
  350. #define do_transform(d,tr,s) ((d)->x = (tr)->o.x + (tr)->sc.x*(s)->x, \
  351. (d)->y = (tr)->o.y + (tr)->sc.y*(s)->y \
  352. + (tr)->sl*(s)->x)
  353. #define do_untransform(d,tr,s) ((d)->x = (.5+(s)->x-(tr)->o.x)/(tr)->sc.x, \
  354. (d)->y = (.5+(s)->y-(tr)->sl*(d)->x-(tr)->o.y) \
  355. /(tr)->sc.y)
  356. #define xtransform(tr,xx) ((tr)->o.x + (tr)->sc.x*(xx))
  357. #define ytransform(tr,yy) ((tr)->o.y + (tr)->sc.y*(yy))
  358. #define dxuntransform(tr,xx) ((xx)/(tr)->sc.x)
  359. #define dyuntransform(tr,yy) ((yy)/(tr)->sc.y)
  360. transform cur_trans(void)
  361. {
  362. transform t;
  363. Rectangle d = screen->r;
  364. const frectangle* s = &univ.disp;
  365. double sh = univ.slant_ht;
  366. d.min.x += lft_border;
  367. d.min.y += top_border;
  368. d.max.x -= rt_border;
  369. d.max.y -= bot_border;
  370. t.sc.x = (d.max.x - d.min.x)/(s->max.x - s->min.x);
  371. t.sc.y = -(d.max.y - d.min.y)/fabs(sh);
  372. if (sh > 0) {
  373. t.sl = -t.sc.y*(s->max.y-s->min.y-sh)/(s->max.x - s->min.x);
  374. t.o.y = d.min.y - t.sc.y*s->max.y - t.sl*s->max.x;
  375. } else {
  376. t.sl = t.sc.y*(s->max.y-s->min.y+sh)/(s->max.x - s->min.x);
  377. t.o.y = d.min.y - t.sc.y*s->max.y - t.sl*s->min.x;
  378. }
  379. t.o.x = d.min.x - t.sc.x*s->min.x;
  380. return t;
  381. }
  382. double u_slant_amt(fpolygons *u)
  383. {
  384. double sh=u->slant_ht, dy=u->disp.max.y - u->disp.min.y;
  385. double dx = u->disp.max.x - u->disp.min.x;
  386. return (sh>0) ? (dy-sh)/dx : -(dy+sh)/dx;
  387. }
  388. /* Set *y0 and *y1 to the lower and upper bounds of the set of y-sl*x values that
  389. *u says to display, where sl is the amount of slant.
  390. */
  391. double set_unslanted_y(fpolygons *u, double *y0, double *y1)
  392. {
  393. double yy1, sl=u_slant_amt(u);
  394. if (u->slant_ht > 0) {
  395. *y0 = u->disp.min.y - sl*u->disp.min.x;
  396. yy1 = *y0 + u->slant_ht;
  397. } else {
  398. yy1 = u->disp.max.y - sl*u->disp.min.x;
  399. *y0 = yy1 + u->slant_ht;
  400. }
  401. if (y1 != 0)
  402. *y1 = yy1;
  403. return sl;
  404. }
  405. /*************************** The region to display ****************************/
  406. void nontrivial_interval(double *lo, double *hi)
  407. {
  408. if (*lo >= *hi) {
  409. double mid = .5*(*lo + *hi);
  410. double tweak = 1e-6 + 1e-6*fabs(mid);
  411. *lo = mid - tweak;
  412. *hi = mid + tweak;
  413. }
  414. }
  415. void init_disp(void)
  416. {
  417. double dw = (univ.bb.max.x - univ.bb.min.x)*underscan;
  418. double dh = (univ.bb.max.y - univ.bb.min.y)*underscan;
  419. univ.disp.min.x = univ.bb.min.x - dw;
  420. univ.disp.min.y = univ.bb.min.y - dh;
  421. univ.disp.max.x = univ.bb.max.x + dw;
  422. univ.disp.max.y = univ.bb.max.y + dh;
  423. nontrivial_interval(&univ.disp.min.x, &univ.disp.max.x);
  424. nontrivial_interval(&univ.disp.min.y, &univ.disp.max.y);
  425. univ.slant_ht = univ.disp.max.y - univ.disp.min.y; /* means no slant */
  426. }
  427. void recenter_disp(Point c)
  428. {
  429. transform tr = cur_trans();
  430. fpoint cc, off;
  431. do_untransform(&cc, &tr, &c);
  432. off.x = cc.x - .5*(univ.disp.min.x + univ.disp.max.x);
  433. off.y = cc.y - .5*(univ.disp.min.y + univ.disp.max.y);
  434. univ.disp.min.x += off.x;
  435. univ.disp.min.y += off.y;
  436. univ.disp.max.x += off.x;
  437. univ.disp.max.y += off.y;
  438. }
  439. /* Find the upper-left and lower-right corners of the bounding box of the
  440. parallelogram formed by untransforming the rectangle rminx, rminy, ... (given
  441. in screen coordinates), and return the height of the parallelogram (negated
  442. if it slopes downward).
  443. */
  444. double untransform_corners(double rminx, double rminy, double rmaxx, double rmaxy,
  445. fpoint *ul, fpoint *lr)
  446. {
  447. fpoint r_ur, r_ul, r_ll, r_lr; /* corners of the given recangle */
  448. fpoint ur, ll; /* untransformed versions of r_ur, r_ll */
  449. transform tr = cur_trans();
  450. double ht;
  451. r_ur.x=rmaxx; r_ur.y=rminy;
  452. r_ul.x=rminx; r_ul.y=rminy;
  453. r_ll.x=rminx; r_ll.y=rmaxy;
  454. r_lr.x=rmaxx; r_lr.y=rmaxy;
  455. do_untransform(ul, &tr, &r_ul);
  456. do_untransform(lr, &tr, &r_lr);
  457. do_untransform(&ur, &tr, &r_ur);
  458. do_untransform(&ll, &tr, &r_ll);
  459. ht = ur.y - lr->y;
  460. if (ll.x < ul->x)
  461. ul->x = ll.x;
  462. if (ur.y > ul->y)
  463. ul->y = ur.y;
  464. else ht = -ht;
  465. if (ur.x > lr->x)
  466. lr->x = ur.x;
  467. if (ll.y < lr->y)
  468. lr->y = ll.y;
  469. return ht;
  470. }
  471. void disp_dozoom(double rminx, double rminy, double rmaxx, double rmaxy)
  472. {
  473. fpoint ul, lr;
  474. double sh = untransform_corners(rminx, rminy, rmaxx, rmaxy, &ul, &lr);
  475. if (ul.x==lr.x || ul.y==lr.y)
  476. return;
  477. univ.slant_ht = sh;
  478. univ.disp.min.x = ul.x;
  479. univ.disp.max.y = ul.y;
  480. univ.disp.max.x = lr.x;
  481. univ.disp.min.y = lr.y;
  482. nontrivial_interval(&univ.disp.min.x, &univ.disp.max.x);
  483. nontrivial_interval(&univ.disp.min.y, &univ.disp.max.y);
  484. }
  485. void disp_zoomin(Rectangle r)
  486. {
  487. disp_dozoom(r.min.x, r.min.y, r.max.x, r.max.y);
  488. }
  489. void disp_zoomout(Rectangle r)
  490. {
  491. double qminx, qminy, qmaxx, qmaxy;
  492. double scx, scy;
  493. Rectangle s = screen->r;
  494. if (r.min.x==r.max.x || r.min.y==r.max.y)
  495. return;
  496. s.min.x += lft_border;
  497. s.min.y += top_border;
  498. s.max.x -= rt_border;
  499. s.max.y -= bot_border;
  500. scx = (s.max.x - s.min.x)/(r.max.x - r.min.x);
  501. scy = (s.max.y - s.min.y)/(r.max.y - r.min.y);
  502. qminx = s.min.x + scx*(s.min.x - r.min.x);
  503. qmaxx = s.max.x + scx*(s.max.x - r.max.x);
  504. qminy = s.min.y + scy*(s.min.y - r.min.y);
  505. qmaxy = s.max.y + scy*(s.max.y - r.max.y);
  506. disp_dozoom(qminx, qminy, qmaxx, qmaxy);
  507. }
  508. void expand2(double* a, double* b, double f)
  509. {
  510. double mid = .5*(*a + *b);
  511. *a = mid + f*(*a - mid);
  512. *b = mid + f*(*b - mid);
  513. }
  514. void disp_squareup(void)
  515. {
  516. double dx = univ.disp.max.x - univ.disp.min.x;
  517. double dy = univ.disp.max.y - univ.disp.min.y;
  518. dx /= screen->r.max.x - lft_border - screen->r.min.x - rt_border;
  519. dy /= screen->r.max.y - bot_border - screen->r.min.y - top_border;
  520. if (dx > dy)
  521. expand2(&univ.disp.min.y, &univ.disp.max.y, dx/dy);
  522. else expand2(&univ.disp.min.x, &univ.disp.max.x, dy/dx);
  523. univ.slant_ht = univ.disp.max.y - univ.disp.min.y;
  524. }
  525. /* Slant so that p and q appear at the same height on the screen and the
  526. screen contains the smallest possible superset of what its previous contents.
  527. */
  528. void slant_disp(fpoint p, fpoint q)
  529. {
  530. double yll, ylr, yul, yur; /* corner y coords of displayed parallelogram */
  531. double sh, dy;
  532. if (p.x == q.x)
  533. return;
  534. sh = univ.slant_ht;
  535. if (sh > 0) {
  536. yll=yul=univ.disp.min.y; yul+=sh;
  537. ylr=yur=univ.disp.max.y; ylr-=sh;
  538. } else {
  539. yll=yul=univ.disp.max.y; yll+=sh;
  540. ylr=yur=univ.disp.min.y; yur-=sh;
  541. }
  542. dy = (univ.disp.max.x-univ.disp.min.x)*(q.y - p.y)/(q.x - p.x);
  543. dy -= ylr - yll;
  544. if (dy > 0)
  545. {yll-=dy; yur+=dy;}
  546. else {yul-=dy; ylr+=dy;}
  547. if (ylr > yll) {
  548. univ.disp.min.y = yll;
  549. univ.disp.max.y = yur;
  550. univ.slant_ht = yur - ylr;
  551. } else {
  552. univ.disp.max.y = yul;
  553. univ.disp.min.y = ylr;
  554. univ.slant_ht = ylr - yur;
  555. }
  556. }
  557. /******************************** Ascii input ********************************/
  558. void set_fbb(fpolygon* fp)
  559. {
  560. fpoint lo=fp->p[0], hi=fp->p[0];
  561. const fpoint *q, *qtop;
  562. for (qtop=(q=fp->p)+fp->n; ++q<=qtop;) {
  563. if (q->x < lo.x) lo.x=q->x;
  564. if (q->y < lo.y) lo.y=q->y;
  565. if (q->x > hi.x) hi.x=q->x;
  566. if (q->y > hi.y) hi.y=q->y;
  567. }
  568. fp->bb.min = lo;
  569. fp->bb.max = hi;
  570. }
  571. char* mystrdup(char* s)
  572. {
  573. char *r, *t = strrchr(s,'"');
  574. if (t==0) {
  575. t = s + strlen(s);
  576. while (t>s && (t[-1]=='\n' || t[-1]=='\r'))
  577. t--;
  578. }
  579. r = malloc(1+(t-s));
  580. memcpy(r, s, t-s);
  581. r[t-s] = 0;
  582. return r;
  583. }
  584. int is_valid_label(char* lab)
  585. {
  586. char* t;
  587. if (lab[0]=='"')
  588. return (t=strrchr(lab,'"'))!=0 && t!=lab && strspn(t+1," \t\r\n")==strlen(t+1);
  589. return strcspn(lab," \t")==strlen(lab);
  590. }
  591. /* Read a polyline and update the number of lines read. A zero result indicates bad
  592. syntax if *lineno increases; otherwise it indicates end of file.
  593. */
  594. fpolygon* rd_fpoly(FILE* fin, int *lineno)
  595. {
  596. char buf[256], junk[2];
  597. fpoint q;
  598. fpolygon* fp;
  599. int allocn;
  600. if (!fgets(buf,256,fin))
  601. return 0;
  602. (*lineno)++;
  603. if (sscanf(buf,"%lg%lg%1s",&q.x,&q.y,junk) != 2)
  604. return 0;
  605. fp = malloc(sizeof(fpolygon));
  606. allocn = 16;
  607. fp->p = malloc(allocn*sizeof(fpoint));
  608. fp->p[0] = q;
  609. fp->n = 0;
  610. fp->nam = "";
  611. fp->thick = 0;
  612. fp->clr = clr_im(DBlack);
  613. while (fgets(buf,256,fin)) {
  614. (*lineno)++;
  615. if (sscanf(buf,"%lg%lg%1s",&q.x,&q.y,junk) != 2) {
  616. if (!is_valid_label(buf))
  617. {free(fp->p); free(fp); return 0;}
  618. fp->nam = (buf[0]=='"') ? buf+1 : buf;
  619. break;
  620. }
  621. if (++(fp->n) == allocn)
  622. fp->p = realloc(fp->p, (allocn<<=1)*sizeof(fpoint));
  623. fp->p[fp->n] = q;
  624. }
  625. fp->nam = mystrdup(fp->nam);
  626. set_fbb(fp);
  627. fp->link = 0;
  628. return fp;
  629. }
  630. /* Read input into *fps and return 0 or a line number where there's a syntax error */
  631. int rd_fpolys(FILE* fin, fpolygons* fps)
  632. {
  633. fpolygon *fp, *fp0=fps->p;
  634. int lineno=0, ok_upto=0;
  635. while ((fp=rd_fpoly(fin,&lineno)) != 0) {
  636. ok_upto = lineno;
  637. fp->link = fps->p;
  638. fps->p = fp;
  639. grow_bb(&fps->bb, &fp->bb);
  640. }
  641. set_default_clrs(fps, fp0);
  642. return (ok_upto==lineno) ? 0 : lineno;
  643. }
  644. /* Read input from file fnam and return an error line no., -1 for "can't open"
  645. or 0 for success.
  646. */
  647. int doinput(char* fnam)
  648. {
  649. FILE* fin = strcmp(fnam,"-")==0 ? stdin : fopen(fnam, "r");
  650. int errline_or0;
  651. if (fin==0)
  652. return -1;
  653. errline_or0 = rd_fpolys(fin, &univ);
  654. fclose(fin);
  655. return errline_or0;
  656. }
  657. /******************************** Ascii output ********************************/
  658. fpolygon* fp_reverse(fpolygon* fp)
  659. {
  660. fpolygon* r = 0;
  661. while (fp!=0) {
  662. fpolygon* q = fp->link;
  663. fp->link = r;
  664. r = fp;
  665. fp = q;
  666. }
  667. return r;
  668. }
  669. void wr_fpoly(FILE* fout, const fpolygon* fp)
  670. {
  671. char buf[256];
  672. int i;
  673. for (i=0; i<=fp->n; i++)
  674. fprintf(fout,"%.12g\t%.12g\n", fp->p[i].x, fp->p[i].y);
  675. fprintf(fout,"\"%s\"\n", nam_with_thclr(fp->nam, fp->thick, fp->clr, buf, 256));
  676. }
  677. void wr_fpolys(FILE* fout, fpolygons* fps)
  678. {
  679. fpolygon* fp;
  680. fps->p = fp_reverse(fps->p);
  681. for (fp=fps->p; fp!=0; fp=fp->link)
  682. wr_fpoly(fout, fp);
  683. fps->p = fp_reverse(fps->p);
  684. }
  685. int dooutput(char* fnam)
  686. {
  687. FILE* fout = fopen(fnam, "w");
  688. if (fout==0)
  689. return 0;
  690. wr_fpolys(fout, &univ);
  691. fclose(fout);
  692. return 1;
  693. }
  694. /************************ Clipping to screen rectangle ************************/
  695. /* Find the t values, 0<=t<=1 for which x0+t*(x1-x0) is between xlo and xhi,
  696. or return 0 to indicate no such t values exist. If returning 1, set *t0 and
  697. *t1 to delimit the t interval.
  698. */
  699. int do_xory(double x0, double x1, double xlo, double xhi, double* t0, double* t1)
  700. {
  701. *t1 = 1.0;
  702. if (x0<xlo) {
  703. if (x1<xlo) return 0;
  704. *t0 = (xlo-x0)/(x1-x0);
  705. if (x1>xhi) *t1 = (xhi-x0)/(x1-x0);
  706. } else if (x0>xhi) {
  707. if (x1>xhi) return 0;
  708. *t0 = (xhi-x0)/(x1-x0);
  709. if (x1<xlo) *t1 = (xlo-x0)/(x1-x0);
  710. } else {
  711. *t0 = 0.0;
  712. if (x1>xhi) *t1 = (xhi-x0)/(x1-x0);
  713. else if (x1<xlo) *t1 = (xlo-x0)/(x1-x0);
  714. else *t1 = 1.0;
  715. }
  716. return 1;
  717. }
  718. /* After mapping y to y-slope*x, what initial fraction of the *p to *q edge is
  719. outside of *r? Note that the edge could start outside *r, pass through *r,
  720. and wind up outside again.
  721. */
  722. double frac_outside(const fpoint* p, const fpoint* q, const frectangle* r,
  723. double slope)
  724. {
  725. double t0, t1, tt0, tt1;
  726. double px=p->x, qx=q->x;
  727. if (!do_xory(px, qx, r->min.x, r->max.x, &t0, &t1))
  728. return 1;
  729. if (!do_xory(p->y-slope*px, q->y-slope*qx, r->min.y, r->max.y, &tt0, &tt1))
  730. return 1;
  731. if (tt0 > t0)
  732. t0 = tt0;
  733. if (t1<=t0 || tt1<=t0)
  734. return 1;
  735. return t0;
  736. }
  737. /* Think of p0..pn as piecewise-linear function F(t) for t=0..pn-p0, and find
  738. the maximum tt such that F(0..tt) is all inside of r, assuming p0 is inside.
  739. Coordinates are transformed by y=y-x*slope before testing against r.
  740. */
  741. double in_length(const fpoint* p0, const fpoint* pn, frectangle r, double slope)
  742. {
  743. const fpoint* p = p0;
  744. double px, py;
  745. do if (++p > pn)
  746. return pn - p0;
  747. while (r.min.x<=(px=p->x) && px<=r.max.x
  748. && r.min.y<=(py=p->y-slope*px) && py<=r.max.y);
  749. return (p - p0) - frac_outside(p, p-1, &r, slope);
  750. }
  751. /* Think of p0..pn as piecewise-linear function F(t) for t=0..pn-p0, and find
  752. the maximum tt such that F(0..tt) is all outside of *r. Coordinates are
  753. transformed by y=y-x*slope before testing against r.
  754. */
  755. double out_length(const fpoint* p0, const fpoint* pn, frectangle r, double slope)
  756. {
  757. const fpoint* p = p0;
  758. double fr;
  759. do { if (p->x < r.min.x)
  760. do if (++p>pn) return pn-p0;
  761. while (p->x <= r.min.x);
  762. else if (p->x > r.max.x)
  763. do if (++p>pn) return pn-p0;
  764. while (p->x >= r.max.x);
  765. else if (p->y-slope*p->x < r.min.y)
  766. do if (++p>pn) return pn-p0;
  767. while (p->y-slope*p->x <= r.min.y);
  768. else if (p->y-slope*p->x > r.max.y)
  769. do if (++p>pn) return pn-p0;
  770. while (p->y-slope*p->x >= r.max.y);
  771. else return p - p0;
  772. } while ((fr=frac_outside(p-1,p,&r,slope)) == 1);
  773. return (p - p0) + fr-1;
  774. }
  775. /*********************** Drawing frame and axis labels ***********************/
  776. #define Nthous 7
  777. #define Len_thous 30 /* bound on strlen(thous_nam[i]) */
  778. char* thous_nam[Nthous] = {
  779. "one", "thousand", "million", "billion",
  780. "trillion", "quadrillion", "quintillion",
  781. };
  782. typedef struct lab_interval {
  783. double sep; /* separation between tick marks */
  784. double unit; /* power of 1000 divisor */
  785. int logunit; /* log base 1000 of of this divisor */
  786. double off; /* offset to subtract before dividing */
  787. } lab_interval;
  788. char* abbrev_num(double x, const lab_interval* iv)
  789. {
  790. static char buf[16];
  791. double dx = x - iv->off;
  792. dx = iv->sep * floor(dx/iv->sep + .5);
  793. sprintf(buf,"%g", dx/iv->unit);
  794. return buf;
  795. }
  796. double lead_digits(double n, double r) /* n truncated to power of 10 above r */
  797. {
  798. double rr = pow(10, ceil(log10(r)));
  799. double nn = (n<rr) ? 0.0 : rr*floor(n/rr);
  800. if (n+r-nn >= digs10pow) {
  801. rr /= 10;
  802. nn = (n<rr) ? 0.0 : rr*floor(n/rr);
  803. }
  804. return nn;
  805. }
  806. lab_interval next_larger(double s0, double xlo, double xhi)
  807. {
  808. double nlo, nhi;
  809. lab_interval r;
  810. r.logunit = (int) floor(log10(s0) + LOG2);
  811. r.unit = pow(10, r.logunit);
  812. nlo = xlo/r.unit;
  813. nhi = xhi/r.unit;
  814. if (nhi >= digs10pow)
  815. r.off = r.unit*lead_digits(nlo, nhi-nlo);
  816. else if (nlo <= -digs10pow)
  817. r.off = -r.unit*lead_digits(-nhi, nhi-nlo);
  818. else r.off = 0;
  819. r.sep = (s0<=r.unit) ? r.unit : (s0<2*r.unit ? 2*r.unit : 5*r.unit);
  820. switch (r.logunit%3) {
  821. case 1: r.unit*=.1; r.logunit--;
  822. break;
  823. case -1: case 2:
  824. r.unit*=10; r.logunit++;
  825. break;
  826. case -2: r.unit*=100; r.logunit+=2;
  827. }
  828. r.logunit /= 3;
  829. return r;
  830. }
  831. double min_hsep(const transform* tr)
  832. {
  833. double s = (2+labdigs)*sdigit.x;
  834. double ss = (univ.disp.min.x<0) ? s+sdigit.x : s;
  835. return dxuntransform(tr, ss);
  836. }
  837. lab_interval mark_x_axis(const transform* tr)
  838. {
  839. fpoint p = univ.disp.min;
  840. Point q, qtop, qbot, tmp;
  841. double x0=univ.disp.min.x, x1=univ.disp.max.x;
  842. double seps0, nseps, seps;
  843. lab_interval iv = next_larger(min_hsep(tr), x0, x1);
  844. set_unslanted_y(&univ, &p.y, 0);
  845. q.y = ytransform(tr, p.y) + .5;
  846. qtop.y = q.y - tick_len;
  847. qbot.y = q.y + framewd + framesep;
  848. seps0 = ceil(x0/iv.sep);
  849. for (seps=0, nseps=floor(x1/iv.sep)-seps0; seps<=nseps; seps+=1) {
  850. char* num = abbrev_num((p.x=iv.sep*(seps0+seps)), &iv);
  851. Font* f = display->defaultfont;
  852. q.x = qtop.x = qbot.x = xtransform(tr, p.x);
  853. line(screen, qtop, q, Enddisc, Enddisc, 0, axis_color, q);
  854. tmp = stringsize(f, num);
  855. qbot.x -= tmp.x/2;
  856. string(screen, qbot, display->black, qbot, f, num);
  857. }
  858. return iv;
  859. }
  860. lab_interval mark_y_axis(const transform* tr)
  861. {
  862. Font* f = display->defaultfont;
  863. fpoint p = univ.disp.min;
  864. Point q, qrt, qlft;
  865. double y0, y1, seps0, nseps, seps;
  866. lab_interval iv;
  867. set_unslanted_y(&univ, &y0, &y1);
  868. iv = next_larger(dyuntransform(tr,-f->height), y0, y1);
  869. q.x = xtransform(tr, p.x) - .5;
  870. qrt.x = q.x + tick_len;
  871. qlft.x = q.x - (framewd + framesep);
  872. seps0 = ceil(y0/iv.sep);
  873. for (seps=0, nseps=floor(y1/iv.sep)-seps0; seps<=nseps; seps+=1) {
  874. char* num = abbrev_num((p.y=iv.sep*(seps0+seps)), &iv);
  875. Point qq = stringsize(f, num);
  876. q.y = qrt.y = qlft.y = ytransform(tr, p.y);
  877. line(screen, qrt, q, Enddisc, Enddisc, 0, axis_color, q);
  878. qq.x = qlft.x - qq.x;
  879. qq.y = qlft.y - qq.y/2;
  880. string(screen, qq, display->black, qq, f, num);
  881. }
  882. return iv;
  883. }
  884. void lab_iv_info(const lab_interval *iv, double slant, char* buf, int *n)
  885. {
  886. if (iv->off > 0)
  887. (*n) += sprintf(buf+*n,"-%.12g",iv->off);
  888. else if (iv->off < 0)
  889. (*n) += sprintf(buf+*n,"+%.12g",-iv->off);
  890. if (slant>0)
  891. (*n) += sprintf(buf+*n,"-%.6gx", slant);
  892. else if (slant<0)
  893. (*n) += sprintf(buf+*n,"+%.6gx", -slant);
  894. if (abs(iv->logunit) >= Nthous)
  895. (*n) += sprintf(buf+*n," in 1e%d units", 3*iv->logunit);
  896. else if (iv->logunit > 0)
  897. (*n) += sprintf(buf+*n," in %ss", thous_nam[iv->logunit]);
  898. else if (iv->logunit < 0)
  899. (*n) += sprintf(buf+*n," in %sths", thous_nam[-iv->logunit]);
  900. }
  901. void draw_xy_ranges(const lab_interval *xiv, const lab_interval *yiv)
  902. {
  903. Point p;
  904. char buf[2*(19+Len_thous+8)+50];
  905. int bufn = 0;
  906. buf[bufn++] = 'x';
  907. lab_iv_info(xiv, 0, buf, &bufn);
  908. bufn += sprintf(buf+bufn, "; y");
  909. lab_iv_info(yiv, u_slant_amt(&univ), buf, &bufn);
  910. buf[bufn] = '\0';
  911. p = stringsize(display->defaultfont, buf);
  912. top_left = screen->r.min.x + lft_border;
  913. p.x = top_right = screen->r.max.x - rt_border - p.x;
  914. p.y = screen->r.min.y + outersep;
  915. string(screen, p, display->black, p, display->defaultfont, buf);
  916. }
  917. transform draw_frame(void)
  918. {
  919. lab_interval x_iv, y_iv;
  920. transform tr;
  921. Rectangle r = screen->r;
  922. lft_border = (univ.disp.min.y<0) ? lft_border0+sdigit.x : lft_border0;
  923. tr = cur_trans();
  924. r.min.x += lft_border;
  925. r.min.y += top_border;
  926. r.max.x -= rt_border;
  927. r.max.y -= bot_border;
  928. border(screen, r, -framewd, axis_color, r.min);
  929. x_iv = mark_x_axis(&tr);
  930. y_iv = mark_y_axis(&tr);
  931. draw_xy_ranges(&x_iv, &y_iv);
  932. return tr;
  933. }
  934. /*************************** Finding the selection ***************************/
  935. typedef struct pt_on_fpoly {
  936. fpoint p; /* the point */
  937. fpolygon* fp; /* the fpolygon it lies on */
  938. double t; /* how many knots from the beginning */
  939. } pt_on_fpoly;
  940. static double myx, myy;
  941. #define mydist(p,o,sl,xwt,ywt) (myx=(p).x-(o).x, myy=(p).y-sl*(p).x-(o).y, \
  942. xwt*myx*myx + ywt*myy*myy)
  943. /* At what fraction of the way from p0[0] to p0[1] is mydist(p,ctr,slant,xwt,ywt)
  944. minimized?
  945. */
  946. double closest_time(const fpoint* p0, const fpoint* ctr, double slant,
  947. double xwt, double ywt)
  948. {
  949. double p00y=p0[0].y-slant*p0[0].x, p01y=p0[1].y-slant*p0[1].x;
  950. double dx=p0[1].x-p0[0].x, dy=p01y-p00y;
  951. double x0=p0[0].x-ctr->x, y0=p00y-ctr->y;
  952. double bot = xwt*dx*dx + ywt*dy*dy;
  953. if (bot==0)
  954. return 0;
  955. return -(xwt*x0*dx + ywt*y0*dy)/bot;
  956. }
  957. /* Scan the polygonal path of length len knots starting at p0, and find the
  958. point that the transformation y=y-x*slant makes closest to the center of *r,
  959. where *r itself defines the distance metric. Knots get higher priority than
  960. points between knots. If psel->t is negative, always update *psel; otherwise
  961. update *psel only if the scan can improve it. Return a boolean that says
  962. whether *psel was updated.
  963. Note that *r is a very tiny rectangle (tiny when converted screen pixels)
  964. such that anything in *r is considered close enough to match the mouse click.
  965. The purpose of this routine is to be careful in case there is a lot of hidden
  966. detail in the tiny rectangle *r.
  967. */
  968. int improve_pt(fpoint* p0, double len, const frectangle* r, double slant,
  969. pt_on_fpoly* psel)
  970. {
  971. fpoint ctr = fcenter(r);
  972. double x_wt=2/(r->max.x-r->min.x), y_wt=2/(r->max.y-r->min.y);
  973. double xwt=x_wt*x_wt, ywt=y_wt*y_wt;
  974. double d, dbest = (psel->t <0) ? 1e30 : mydist(psel->p,ctr,slant,xwt,ywt);
  975. double tt, dbest0 = dbest;
  976. fpoint pp;
  977. int ilen = (int) len;
  978. if (len==0 || ilen>0) {
  979. int i;
  980. for (i=(len==0 ? 0 : 1); i<=ilen; i++) {
  981. d = mydist(p0[i], ctr, slant, xwt, ywt);
  982. if (d < dbest)
  983. {psel->p=p0[i]; psel->t=i; dbest=d;}
  984. }
  985. return (dbest < dbest0);
  986. }
  987. tt = closest_time(p0, &ctr, slant, xwt, ywt);
  988. if (tt > len)
  989. tt = len;
  990. pp.x = p0[0].x + tt*(p0[1].x - p0[0].x);
  991. pp.y = p0[0].y + tt*(p0[1].y - p0[0].y);
  992. if (mydist(pp, ctr, slant, xwt, ywt) < dbest) {
  993. psel->p = pp;
  994. psel->t = tt;
  995. return 1;
  996. }
  997. return 0;
  998. }
  999. /* Test *fp against *r after transforming by y=y-x*slope, and set *psel accordingly.
  1000. */
  1001. void select_in_fpoly(fpolygon* fp, const frectangle* r, double slant,
  1002. pt_on_fpoly* psel)
  1003. {
  1004. fpoint *p0=fp->p, *pn=fp->p+fp->n;
  1005. double l1, l2;
  1006. if (p0==pn)
  1007. {improve_pt(p0, 0, r, slant, psel); psel->fp=fp; return;}
  1008. while ((l1=out_length(p0,pn,*r,slant)) < pn-p0) {
  1009. fpoint p0sav;
  1010. int i1 = (int) l1;
  1011. p0+=i1; l1-=i1;
  1012. p0sav = *p0;
  1013. p0[0].x += l1*(p0[1].x - p0[0].x);
  1014. p0[0].y += l1*(p0[1].y - p0[0].y);
  1015. l2 = in_length(p0, pn, *r, slant);
  1016. if (improve_pt(p0, l2, r, slant, psel)) {
  1017. if (l1==0 && psel->t!=((int) psel->t)) {
  1018. psel->t = 0;
  1019. psel->p = *p0;
  1020. } else if (psel->t < 1)
  1021. psel->t += l1*(1 - psel->t);
  1022. psel->t += p0 - fp->p;
  1023. psel->fp = fp;
  1024. }
  1025. *p0 = p0sav;
  1026. p0 += (l2>0) ? ((int) ceil(l2)) : 1;
  1027. }
  1028. }
  1029. /* Test all the fpolygons against *r after transforming by y=y-x*slope, and return
  1030. the resulting selection, if any.
  1031. */
  1032. pt_on_fpoly* select_in_univ(const frectangle* r, double slant)
  1033. {
  1034. static pt_on_fpoly answ;
  1035. fpolygon* fp;
  1036. answ.t = -1;
  1037. for (fp=univ.p; fp!=0; fp=fp->link)
  1038. if (fintersects(r, &fp->bb, slant))
  1039. select_in_fpoly(fp, r, slant, &answ);
  1040. if (answ.t < 0)
  1041. return 0;
  1042. return &answ;
  1043. }
  1044. /**************************** Using the selection ****************************/
  1045. pt_on_fpoly cur_sel; /* current selection if cur_sel.t>=0 */
  1046. pt_on_fpoly prev_sel; /* previous selection if prev_sel.t>=0 (for slant) */
  1047. Image* sel_bkg = 0; /* what's behind the red dot */
  1048. void clear_txt(void)
  1049. {
  1050. Rectangle r;
  1051. r.min = screen->r.min;
  1052. r.min.x += lft_border;
  1053. r.min.y += outersep;
  1054. r.max.x = top_left;
  1055. r.max.y = r.min.y + smaxch.y;
  1056. draw(screen, r, display->white, display->opaque, r.min);
  1057. top_left = r.min.x;
  1058. }
  1059. Rectangle sel_dot_box(const transform* tr)
  1060. {
  1061. Point ctr;
  1062. Rectangle r;
  1063. if (tr==0)
  1064. ctr.x = ctr.y = Dotrad;
  1065. else do_transform(&ctr, tr, &cur_sel.p);
  1066. r.min.x=ctr.x-Dotrad; r.max.x=ctr.x+Dotrad+1;
  1067. r.min.y=ctr.y-Dotrad; r.max.y=ctr.y+Dotrad+1;
  1068. return r;
  1069. }
  1070. void unselect(const transform* tr)
  1071. {
  1072. transform tra;
  1073. if (sel_bkg==0)
  1074. sel_bkg = allocimage(display, sel_dot_box(0), CMAP8, 0, DWhite);
  1075. clear_txt();
  1076. if (cur_sel.t < 0)
  1077. return;
  1078. prev_sel = cur_sel;
  1079. if (tr==0)
  1080. {tra=cur_trans(); tr=&tra;}
  1081. draw(screen, sel_dot_box(tr), sel_bkg, display->opaque, ZP);
  1082. cur_sel.t = -1;
  1083. }
  1084. /* Text at top right is written first and this low-level routine clobbers it if
  1085. the new top-left text would overwrite it. However, users of this routine should
  1086. try to keep the new text short enough to avoid this.
  1087. */
  1088. void show_mytext(char* msg)
  1089. {
  1090. Point tmp, pt = screen->r.min;
  1091. int siz;
  1092. tmp = stringsize(display->defaultfont, msg);
  1093. siz = tmp.x;
  1094. pt.x=top_left; pt.y+=outersep;
  1095. if (top_left+siz > top_right) {
  1096. Rectangle r;
  1097. r.min.y = pt.y;
  1098. r.min.x = top_right;
  1099. r.max.y = r.min.y + smaxch.y;
  1100. r.max.x = top_left+siz;
  1101. draw(screen, r, display->white, display->opaque, r.min);
  1102. top_right = top_left+siz;
  1103. }
  1104. string(screen, pt, display->black, ZP, display->defaultfont, msg);
  1105. top_left += siz;
  1106. }
  1107. double rnd(double x, double tol) /* round to enough digits for accuracy tol */
  1108. {
  1109. double t = pow(10, floor(log10(tol)));
  1110. return t * floor(x/t + .5);
  1111. }
  1112. double t_tol(double xtol, double ytol)
  1113. {
  1114. int t = (int) floor(cur_sel.t);
  1115. fpoint* p = cur_sel.fp->p;
  1116. double dx, dy;
  1117. if (t==cur_sel.t)
  1118. return 1;
  1119. dx = fabs(p[t+1].x - p[t].x);
  1120. dy = fabs(p[t+1].y - p[t].y);
  1121. xtol /= (xtol>dx) ? xtol : dx;
  1122. ytol /= (ytol>dy) ? ytol : dy;
  1123. return (xtol<ytol) ? xtol : ytol;
  1124. }
  1125. void say_where(const transform* tr)
  1126. {
  1127. double xtol=dxuntransform(tr,1), ytol=dyuntransform(tr,-1);
  1128. char buf[100];
  1129. int n, nmax = (top_right - top_left)/smaxch.x;
  1130. if (nmax >= 100)
  1131. nmax = 100-1;
  1132. n = sprintf(buf,"(%.14g,%.14g) at t=%.14g",
  1133. rnd(cur_sel.p.x,xtol), rnd(cur_sel.p.y,ytol),
  1134. rnd(cur_sel.t, t_tol(xtol,ytol)));
  1135. if (cur_sel.fp->nam[0] != 0)
  1136. sprintf(buf+n," %.*s", nmax-n-1, cur_sel.fp->nam);
  1137. show_mytext(buf);
  1138. }
  1139. void reselect(const transform* tr) /* uselect(); set cur_sel; call this */
  1140. {
  1141. Point pt2, pt3;
  1142. fpoint p2;
  1143. transform tra;
  1144. if (cur_sel.t < 0)
  1145. return;
  1146. if (tr==0)
  1147. {tra=cur_trans(); tr=&tra;}
  1148. do_transform(&p2, tr, &cur_sel.p);
  1149. if (fabs(p2.x)+fabs(p2.y)>1e8 || (pt2.x=p2.x, pt2.y=p2.y, is_off_screen(pt2)))
  1150. {cur_sel.t= -1; return;}
  1151. pt3.x=pt2.x-Dotrad; pt3.y=pt2.y-Dotrad;
  1152. draw(sel_bkg, sel_dot_box(0), screen, display->opaque, pt3);
  1153. fillellipse(screen, pt2, Dotrad, Dotrad, clr_im(DRed), pt2);
  1154. say_where(tr);
  1155. }
  1156. void do_select(Point pt)
  1157. {
  1158. transform tr = cur_trans();
  1159. fpoint pt1, pt2, ctr;
  1160. frectangle r;
  1161. double slant;
  1162. pt_on_fpoly* psel;
  1163. unselect(&tr);
  1164. do_untransform(&ctr, &tr, &pt);
  1165. pt1.x=pt.x-fuzz; pt1.y=pt.y+fuzz;
  1166. pt2.x=pt.x+fuzz; pt2.y=pt.y-fuzz;
  1167. do_untransform(&r.min, &tr, &pt1);
  1168. do_untransform(&r.max, &tr, &pt2);
  1169. slant = u_slant_amt(&univ);
  1170. slant_frect(&r, -slant);
  1171. psel = select_in_univ(&r, slant);
  1172. if (psel==0)
  1173. return;
  1174. if (logfil!=0) {
  1175. fprintf(logfil,"%.14g\t%.14g\n", psel->p.x, psel->p.y);
  1176. fflush(logfil);
  1177. }
  1178. cur_sel = *psel;
  1179. reselect(&tr);
  1180. }
  1181. /***************************** Prompting for text *****************************/
  1182. void unshow_mytext(char* msg)
  1183. {
  1184. Rectangle r;
  1185. Point siz = stringsize(display->defaultfont, msg);
  1186. top_left -= siz.x;
  1187. r.min.y = screen->r.min.y + outersep;
  1188. r.min.x = top_left;
  1189. r.max.y = r.min.y + siz.y;
  1190. r.max.x = r.min.x + siz.x;
  1191. draw(screen, r, display->white, display->opaque, r.min);
  1192. }
  1193. /* Show the given prompt and read a line of user input. The text appears at the
  1194. top left. If it runs into the top right text, we stop echoing but let the user
  1195. continue typing blind if he wants to.
  1196. */
  1197. char* prompt_text(char* prompt)
  1198. {
  1199. static char buf[200];
  1200. int n0, n=0, nshown=0;
  1201. Rune c;
  1202. unselect(0);
  1203. show_mytext(prompt);
  1204. while (n<200-1-UTFmax && (c=ekbd())!='\n') {
  1205. if (c=='\b') {
  1206. buf[n] = 0;
  1207. if (n > 0)
  1208. do n--;
  1209. while (n>0 && (buf[n-1]&0xc0)==0x80);
  1210. if (n < nshown)
  1211. {unshow_mytext(buf+n); nshown=n;}
  1212. } else {
  1213. n0 = n;
  1214. n += runetochar(buf+n, &c);
  1215. buf[n] = 0;
  1216. if (nshown==n0 && top_right-top_left >= smaxch.x)
  1217. {show_mytext(buf+n0); nshown=n;}
  1218. }
  1219. }
  1220. buf[n] = 0;
  1221. while (ecanmouse())
  1222. emouse();
  1223. return buf;
  1224. }
  1225. /**************************** Redrawing the screen ****************************/
  1226. /* Let p0 and its successors define a piecewise-linear function of a paramter t,
  1227. and draw the 0<=t<=n1 portion using transform *tr.
  1228. */
  1229. void draw_fpts(const fpoint* p0, double n1, const transform* tr, int thick,
  1230. Image* clr)
  1231. {
  1232. int n = (int) n1;
  1233. const fpoint* p = p0 + n;
  1234. fpoint pp;
  1235. Point qq, q;
  1236. if (n1 > n) {
  1237. pp.x = p[0].x + (n1-n)*(p[1].x - p[0].x);
  1238. pp.y = p[0].y + (n1-n)*(p[1].y - p[0].y);
  1239. } else pp = *p--;
  1240. do_transform(&qq, tr, &pp);
  1241. if (n1==0)
  1242. fillellipse(screen, qq, 1+thick, 1+thick, clr, qq);
  1243. for (; p>=p0; p--) {
  1244. do_transform(&q, tr, p);
  1245. line(screen, qq, q, Enddisc, Enddisc, thick, clr, qq);
  1246. qq = q;
  1247. }
  1248. }
  1249. void draw_1fpoly(const fpolygon* fp, const transform* tr, Image* clr,
  1250. const frectangle *udisp, double slant)
  1251. {
  1252. fpoint *p0=fp->p, *pn=fp->p+fp->n;
  1253. double l1, l2;
  1254. if (p0==pn && fcontains(udisp,*p0))
  1255. {draw_fpts(p0, 0, tr, fp->thick, clr); return;}
  1256. while ((l1=out_length(p0,pn,*udisp,slant)) < pn-p0) {
  1257. fpoint p0sav;
  1258. int i1 = (int) l1;
  1259. p0+=i1; l1-=i1;
  1260. p0sav = *p0;
  1261. p0[0].x += l1*(p0[1].x - p0[0].x);
  1262. p0[0].y += l1*(p0[1].y - p0[0].y);
  1263. l2 = in_length(p0, pn, *udisp, slant);
  1264. draw_fpts(p0, l2, tr, fp->thick, clr);
  1265. *p0 = p0sav;
  1266. p0 += (l2>0) ? ((int) ceil(l2)) : 1;
  1267. }
  1268. }
  1269. double get_clip_data(const fpolygons *u, frectangle *r)
  1270. {
  1271. double slant = set_unslanted_y(u, &r->min.y, &r->max.y);
  1272. r->min.x = u->disp.min.x;
  1273. r->max.x = u->disp.max.x;
  1274. return slant;
  1275. }
  1276. void draw_fpoly(const fpolygon* fp, const transform* tr, Image* clr)
  1277. {
  1278. frectangle r;
  1279. double slant = get_clip_data(&univ, &r);
  1280. draw_1fpoly(fp, tr, clr, &r, slant);
  1281. }
  1282. void eresized(int new)
  1283. {
  1284. transform tr;
  1285. fpolygon* fp;
  1286. frectangle clipr;
  1287. double slant;
  1288. if(new && getwindow(display, Refmesg) < 0) {
  1289. fprintf(stderr,"can't reattach to window\n");
  1290. exits("reshap");
  1291. }
  1292. draw(screen, screen->r, display->white, display->opaque, screen->r.min);
  1293. tr = draw_frame();
  1294. slant = get_clip_data(&univ, &clipr);
  1295. for (fp=univ.p; fp!=0; fp=fp->link)
  1296. if (fintersects(&clipr, &fp->bb, slant))
  1297. draw_1fpoly(fp, &tr, fp->clr, &clipr, slant);
  1298. reselect(0);
  1299. if (mv_bkgd!=0 && mv_bkgd->repl==0) {
  1300. freeimage(mv_bkgd);
  1301. mv_bkgd = display->white;
  1302. }
  1303. flushimage(display, 1);
  1304. }
  1305. /********************************* Recoloring *********************************/
  1306. int draw_palette(int n) /* n is number of colors; returns patch dy */
  1307. {
  1308. int y0 = screen->r.min.y + top_border;
  1309. int dy = (screen->r.max.y - bot_border - y0)/n;
  1310. Rectangle r;
  1311. int i;
  1312. r.min.y = y0;
  1313. r.min.x = screen->r.max.x - rt_border + framewd;
  1314. r.max.y = y0 + dy;
  1315. r.max.x = screen->r.max.x;
  1316. for (i=0; i<n; i++) {
  1317. draw(screen, r, clrtab[i].im, display->opaque, r.min);
  1318. r.min.y = r.max.y;
  1319. r.max.y += dy;
  1320. }
  1321. return dy;
  1322. }
  1323. Image* palette_color(Point pt, int dy, int n)
  1324. { /* mouse at pt, patch size dy, n colors */
  1325. int yy;
  1326. if (screen->r.max.x - pt.x > rt_border - framewd)
  1327. return 0;
  1328. yy = pt.y - (screen->r.min.y + top_border);
  1329. if (yy<0 || yy>=n*dy)
  1330. return 0;
  1331. return clrtab[yy/dy].im;
  1332. }
  1333. void all_set_clr(fpolygons* fps, Image* clr)
  1334. {
  1335. fpolygon* p;
  1336. for (p=fps->p; p!=0; p=p->link)
  1337. p->clr = clr;
  1338. }
  1339. void do_recolor(int but, Mouse* m, int alluniv)
  1340. {
  1341. int nclr = clr_id(DWhite);
  1342. int dy = draw_palette(nclr);
  1343. Image* clr;
  1344. if (!get_1click(but, m, 0)) {
  1345. eresized(0);
  1346. return;
  1347. }
  1348. clr = palette_color(m->xy, dy, nclr);
  1349. if (clr != 0) {
  1350. if (alluniv)
  1351. all_set_clr(&univ, clr);
  1352. else cur_sel.fp->clr = clr;
  1353. }
  1354. eresized(0);
  1355. lift_button(but, m, Never);
  1356. }
  1357. /****************************** Move and rotate ******************************/
  1358. void prepare_mv(const fpolygon* fp)
  1359. {
  1360. Rectangle r = screen->r;
  1361. Image* scr0;
  1362. int dt = 1 + fp->thick;
  1363. r.min.x+=lft_border-dt; r.min.y+=top_border-dt;
  1364. r.max.x-=rt_border-dt; r.max.y-=bot_border-dt;
  1365. if (mv_bkgd!=0 && mv_bkgd->repl==0)
  1366. freeimage(mv_bkgd);
  1367. mv_bkgd = allocimage(display, r, CMAP8, 0, DNofill);
  1368. if (mv_bkgd==0)
  1369. mv_bkgd = display->white;
  1370. else { transform tr = cur_trans();
  1371. draw(mv_bkgd, r, screen, display->opaque, r.min);
  1372. draw(mv_bkgd, sel_dot_box(&tr), sel_bkg, display->opaque, ZP);
  1373. scr0 = screen;
  1374. screen = mv_bkgd;
  1375. draw_fpoly(fp, &tr, display->white);
  1376. screen = scr0;
  1377. }
  1378. }
  1379. void move_fp(fpolygon* fp, double dx, double dy)
  1380. {
  1381. fpoint *p, *pn=fp->p+fp->n;
  1382. for (p=fp->p; p<=pn; p++) {
  1383. (p->x) += dx;
  1384. (p->y) += dy;
  1385. }
  1386. (fp->bb.min.x)+=dx; (fp->bb.min.y)+=dy;
  1387. (fp->bb.max.x)+=dx; (fp->bb.max.y)+=dy;
  1388. }
  1389. void rotate_fp(fpolygon* fp, fpoint o, double theta)
  1390. {
  1391. double s=sin(theta), c=cos(theta);
  1392. fpoint *p, *pn=fp->p+fp->n;
  1393. for (p=fp->p; p<=pn; p++) {
  1394. double x=p->x-o.x, y=p->y-o.y;
  1395. (p->x) = o.x + c*x - s*y;
  1396. (p->y) = o.y + s*x + c*y;
  1397. }
  1398. set_fbb(fp);
  1399. }
  1400. /* Move the selected fpolygon so the selected point tracks the mouse, and return
  1401. the total amount of movement. Button but has already been held down for at
  1402. least Mv_delay milliseconds and the mouse might have moved some distance.
  1403. */
  1404. fpoint do_move(int but, Mouse* m)
  1405. {
  1406. transform tr = cur_trans();
  1407. int bbit = Button_bit(but);
  1408. fpolygon* fp = cur_sel.fp;
  1409. fpoint loc, loc0=cur_sel.p;
  1410. double tsav = cur_sel.t;
  1411. unselect(&tr);
  1412. do { latest_mouse(but, m);
  1413. (fp->thick)++; /* line() DISAGREES WITH ITSELF */
  1414. draw_fpoly(fp, &tr, mv_bkgd);
  1415. (fp->thick)--;
  1416. do_untransform(&loc, &tr, &m->xy);
  1417. move_fp(fp, loc.x-cur_sel.p.x, loc.y-cur_sel.p.y);
  1418. cur_sel.p = loc;
  1419. draw_fpoly(fp, &tr, fp->clr);
  1420. } while (m->buttons & bbit);
  1421. cur_sel.t = tsav;
  1422. reselect(&tr);
  1423. loc.x -= loc0.x;
  1424. loc.y -= loc0.y;
  1425. return loc;
  1426. }
  1427. double dir_angle(const Point* pt, const transform* tr)
  1428. {
  1429. fpoint p;
  1430. double dy, dx;
  1431. do_untransform(&p, tr, pt);
  1432. dy=p.y-cur_sel.p.y; dx=p.x-cur_sel.p.x;
  1433. return (dx==0 && dy==0) ? 0.0 : atan2(dy, dx);
  1434. }
  1435. /* Rotate the selected fpolygon around the selection point so as to track the
  1436. direction angle from the selected point to m->xy. Stop when button but goes
  1437. up and return the total amount of rotation in radians.
  1438. */
  1439. double do_rotate(int but, Mouse* m)
  1440. {
  1441. transform tr = cur_trans();
  1442. int bbit = Button_bit(but);
  1443. fpolygon* fp = cur_sel.fp;
  1444. double theta0 = dir_angle(&m->xy, &tr);
  1445. double th, theta = theta0;
  1446. do { latest_mouse(but, m);
  1447. (fp->thick)++; /* line() DISAGREES WITH ITSELF */
  1448. draw_fpoly(fp, &tr, mv_bkgd);
  1449. (fp->thick)--;
  1450. th = dir_angle(&m->xy, &tr);
  1451. rotate_fp(fp, cur_sel.p, th-theta);
  1452. theta = th;
  1453. draw_fpoly(fp, &tr, fp->clr);
  1454. } while (m->buttons & bbit);
  1455. unselect(&tr);
  1456. cur_sel = prev_sel;
  1457. reselect(&tr);
  1458. return theta - theta0;
  1459. }
  1460. /********************************* Edit menu *********************************/
  1461. typedef enum e_index {
  1462. Erecolor, Ethick, Edelete, Eundo, Erotate, Eoptions,
  1463. Emove
  1464. } e_index;
  1465. char* e_items[Eoptions+1];
  1466. Menu e_menu = {e_items, 0, 0};
  1467. typedef struct e_action {
  1468. e_index typ; /* What type of action */
  1469. fpolygon* fp; /* fpolygon the action applies to */
  1470. Image* clr; /* color to use if typ==Erecolor */
  1471. double amt; /* rotation angle or line thickness */
  1472. fpoint pt; /* movement vector or rotation center */
  1473. struct e_action* link; /* next in a stack */
  1474. } e_action;
  1475. e_action* unact = 0; /* heads a linked list of actions */
  1476. e_action* do_undo(e_action*); /* pop off an e_action and (un)do it */
  1477. e_action* save_act(e_action*,e_index); /* append new e_action for status quo */
  1478. void save_mv(fpoint movement)
  1479. {
  1480. unact = save_act(unact, Emove);
  1481. unact->pt = movement;
  1482. }
  1483. void init_e_menu(void)
  1484. {
  1485. char* u = "can't undo";
  1486. e_items[Erecolor] = "recolor";
  1487. e_items[Edelete] = "delete";
  1488. e_items[Erotate] = "rotate";
  1489. e_items[Eoptions-cantmv] = 0;
  1490. e_items[Ethick] = (cur_sel.fp->thick >0) ? "thin" : "thick";
  1491. if (unact!=0)
  1492. switch (unact->typ) {
  1493. case Erecolor: u="uncolor"; break;
  1494. case Ethick: u=(unact->fp->thick==0) ? "unthin" : "unthicken";
  1495. break;
  1496. case Edelete: u="undelete"; break;
  1497. case Emove: u="unmove"; break;
  1498. case Erotate: u="unrotate"; break;
  1499. }
  1500. e_items[Eundo] = u;
  1501. }
  1502. void do_emenu(int but, Mouse* m)
  1503. {
  1504. int h;
  1505. if (cur_sel.t < 0)
  1506. return;
  1507. init_e_menu();
  1508. h = emenuhit(but, m, &e_menu);
  1509. switch(h) {
  1510. case Ethick: unact = save_act(unact, h);
  1511. cur_sel.fp->thick ^= 1;
  1512. eresized(0);
  1513. break;
  1514. case Edelete: unact = save_act(unact, h);
  1515. fp_remove(&univ, cur_sel.fp);
  1516. unselect(0);
  1517. eresized(0);
  1518. break;
  1519. case Erecolor: unact = save_act(unact, h);
  1520. do_recolor(but, m, 0);
  1521. break;
  1522. case Erotate: unact = save_act(unact, h);
  1523. prepare_mv(cur_sel.fp);
  1524. if (get_1click(but, m, 0)) {
  1525. unact->pt = cur_sel.p;
  1526. unact->amt = do_rotate(but, m);
  1527. }
  1528. break;
  1529. case Eundo: unact = do_undo(unact);
  1530. break;
  1531. }
  1532. }
  1533. /******************************* Undoing edits *******************************/
  1534. e_action* save_act(e_action* a0, e_index typ)
  1535. { /* append new e_action for status quo */
  1536. e_action* a = malloc(sizeof(e_action));
  1537. a->link = a0;
  1538. a->pt.x = a->pt.y = 0.0;
  1539. a->amt = cur_sel.fp->thick;
  1540. a->clr = cur_sel.fp->clr;
  1541. a->fp = cur_sel.fp;
  1542. a->typ = typ;
  1543. return a;
  1544. }
  1545. /* This would be trivial except it's nice to preserve the selection in order to make
  1546. it easy to undo a series of moves. (There's no do_unrotate() because it's harder
  1547. and less important to preserve the selection in that case.)
  1548. */
  1549. void do_unmove(e_action* a)
  1550. {
  1551. double tsav = cur_sel.t;
  1552. unselect(0);
  1553. move_fp(a->fp, -a->pt.x, -a->pt.y);
  1554. if (a->fp == cur_sel.fp) {
  1555. cur_sel.p.x -= a->pt.x;
  1556. cur_sel.p.y -= a->pt.y;
  1557. }
  1558. cur_sel.t = tsav;
  1559. reselect(0);
  1560. }
  1561. e_action* do_undo(e_action* a0) /* pop off an e_action and (un)do it */
  1562. {
  1563. e_action* a = a0;
  1564. if (a==0)
  1565. return 0;
  1566. switch(a->typ) {
  1567. case Ethick: a->fp->thick = a->amt;
  1568. eresized(0);
  1569. break;
  1570. case Erecolor: a->fp->clr = a->clr;
  1571. eresized(0);
  1572. break;
  1573. case Edelete:
  1574. a->fp->link = univ.p;
  1575. univ.p = a->fp;
  1576. grow_bb(&univ.bb, &a->fp->bb);
  1577. eresized(0);
  1578. break;
  1579. case Emove:
  1580. do_unmove(a);
  1581. eresized(0);
  1582. break;
  1583. case Erotate:
  1584. unselect(0);
  1585. rotate_fp(a->fp, a->pt, -a->amt);
  1586. eresized(0);
  1587. break;
  1588. }
  1589. a0 = a->link;
  1590. free(a);
  1591. return a0;
  1592. }
  1593. /********************************* Main menu *********************************/
  1594. enum m_index { Mzoom_in, Mzoom_out, Munzoom, Mslant, Munslant,
  1595. Msquare_up, Mrecenter, Mrecolor, Mrestack, Mread,
  1596. Mwrite, Mexit};
  1597. char* m_items[] = {"zoom in", "zoom out", "unzoom", "slant", "unslant",
  1598. "square up", "recenter", "recolor", "restack", "read",
  1599. "write", "exit", 0};
  1600. Menu m_menu = {m_items, 0, 0};
  1601. void do_mmenu(int but, Mouse* m)
  1602. {
  1603. int e, h = emenuhit(but, m, &m_menu);
  1604. switch (h) {
  1605. case Mzoom_in:
  1606. disp_zoomin(egetrect(but,m));
  1607. eresized(0);
  1608. break;
  1609. case Mzoom_out:
  1610. disp_zoomout(egetrect(but,m));
  1611. eresized(0);
  1612. break;
  1613. case Msquare_up:
  1614. disp_squareup();
  1615. eresized(0);
  1616. break;
  1617. case Munzoom:
  1618. init_disp();
  1619. eresized(0);
  1620. break;
  1621. case Mrecenter:
  1622. if (get_1click(but, m, &bullseye)) {
  1623. recenter_disp(m->xy);
  1624. eresized(0);
  1625. lift_button(but, m, Never);
  1626. }
  1627. break;
  1628. case Mslant:
  1629. if (cur_sel.t>=0 && prev_sel.t>=0) {
  1630. slant_disp(prev_sel.p, cur_sel.p);
  1631. eresized(0);
  1632. }
  1633. break;
  1634. case Munslant:
  1635. univ.slant_ht = univ.disp.max.y - univ.disp.min.y;
  1636. eresized(0);
  1637. break;
  1638. case Mrecolor:
  1639. do_recolor(but, m, 1);
  1640. break;
  1641. case Mrestack:
  1642. fps_invert(&univ);
  1643. eresized(0);
  1644. break;
  1645. case Mread:
  1646. e = doinput(prompt_text("File:"));
  1647. if (e==0)
  1648. eresized(0);
  1649. else if (e<0)
  1650. show_mytext(" - can't read");
  1651. else {
  1652. char ebuf[80];
  1653. snprintf(ebuf, 80, " - error line %d", e);
  1654. show_mytext(ebuf);
  1655. }
  1656. break;
  1657. case Mwrite:
  1658. if (!dooutput(prompt_text("File:")))
  1659. show_mytext(" - can't write");
  1660. break;
  1661. case Mexit:
  1662. exits("");
  1663. }
  1664. }
  1665. /****************************** Handling events ******************************/
  1666. void doevent(void)
  1667. {
  1668. ulong etype;
  1669. int mobile;
  1670. ulong mvtime;
  1671. Event ev;
  1672. etype = eread(Emouse|Ekeyboard, &ev);
  1673. if(etype & Emouse) {
  1674. if (ev.mouse.buttons & But1) {
  1675. do_select(ev.mouse.xy);
  1676. mvtime = Never;
  1677. mobile = !cantmv && cur_sel.t>=0;
  1678. if (mobile) {
  1679. mvtime = ev.mouse.msec + Mv_delay;
  1680. prepare_mv(cur_sel.fp);
  1681. }
  1682. if (!lift_button(1, &ev.mouse, mvtime) && mobile)
  1683. save_mv(do_move(1, &ev.mouse));
  1684. } else if (ev.mouse.buttons & But2)
  1685. do_emenu(2, &ev.mouse);
  1686. else if (ev.mouse.buttons & But3)
  1687. do_mmenu(3, &ev.mouse);
  1688. }
  1689. /* no need to check (etype & Ekeyboard)--there are no keyboard commands */
  1690. }
  1691. /******************************** Main program ********************************/
  1692. extern char* argv0;
  1693. void usage(void)
  1694. {
  1695. int i;
  1696. fprintf(stderr,"Usage %s [options] [infile]\n", argv0);
  1697. fprintf(stderr,
  1698. "option ::= -l logfile | -m\n"
  1699. "\n"
  1700. "Read a polygonal line graph in an ASCII format (one x y pair per line, delimited\n"
  1701. "by spaces with a label after each polyline), and view it interactively. Use\n"
  1702. "standard input if no infile is specified.\n"
  1703. "Option -l specifies a file in which to log the coordinates of each point selected.\n"
  1704. "(Clicking a point with button one selects it and displays its coordinates and\n"
  1705. "the label of its polylone.) Option -m allows polylines to be moved and rotated.\n"
  1706. "The polyline labels can use the following color names:"
  1707. );
  1708. for (i=0; clrtab[i].c!=DNofill; i++)
  1709. fprintf(stderr,"%s%8s", (i%8==0 ? "\n" : " "), clrtab[i].nam);
  1710. fputc('\n', stderr);
  1711. exits("usage");
  1712. }
  1713. void main(int argc, char *argv[])
  1714. {
  1715. int e;
  1716. ARGBEGIN {
  1717. case 'm': cantmv=0;
  1718. break;
  1719. case 'l': logfil = fopen(ARGF(),"w");
  1720. break;
  1721. default: usage();
  1722. } ARGEND
  1723. if(initdraw(0, 0, "gview") < 0)
  1724. exits("initdraw");
  1725. einit(Emouse|Ekeyboard);
  1726. do {
  1727. e = doinput(*argv ? *argv : "-");
  1728. if (e < 0) {
  1729. fprintf(stderr,"Cannot read input file %s\n", *argv);
  1730. exits("no valid input file");
  1731. } else if (e > 0) {
  1732. fprintf(stderr,"Bad syntax at line %d of file %s\n", e, *argv ? *argv : "-");
  1733. exits("bad syntax in input");
  1734. }
  1735. } while (*argv && *++argv);
  1736. init_disp();
  1737. init_clrtab();
  1738. set_default_clrs(&univ, 0);
  1739. adjust_border(display->defaultfont);
  1740. cur_sel.t = prev_sel.t = -1;
  1741. eresized(0);
  1742. for(;;)
  1743. doevent();
  1744. }