main() { float ship[XY]; long org[XY]; long size[XY]; Device dev; short val; Device mdev[XY]; short mval[XY]; long nhits; short buffer[BUFSIZE]; Boolean run; prefsize(400, 400); winopen("select1"); getorigin(&org[X], &org[Y]); getsize(&size[X], &size[Y]); mmode(MVIEWING); ortho2(-0.5, size[X] - 0.5, -0.5, size[Y] - 0.5); qdevice(LEFTMOUSE); qdevice(ESCKEY); color(BLACK); clear(); mdev[X] = MOUSEX; mdev[Y] = MOUSEY; drawplanet(); run = TRUE; while (run) { dev = qread(&val); if (val == 0) { /* on upstroke */ switch (dev) { case LEFTMOUSE: getdev(XY, mdev, mval); ship[X] = mval[X] - org[X]; ship[Y] = mval[Y] - org[Y]; color(BLUE); sbox(ship[X], ship[Y], ship[X] + SHIPWIDTH, ship[Y] + SHIPHEIGHT); /* * specify the selecting region to be a box surrounding the * rocket ship */ ortho2(ship[X], ship[X] + SHIPWIDTH, ship[Y], ship[Y] + SHIPHEIGHT); initnames(); gselect(buffer, BUFSIZE); loadname(PLANET); /* no actual drawing takes place */ drawplanet(); nhits = endselect(buffer); /* * restore the Projection matrix; NB. can't use push/popmatrix * since they only work for the ModelView matrix stack * when in MVIEWING mode */ ortho2(-0.5, size[X] - 0.5, -0.5, size[Y] - 0.5); /* * check to see if PLANET was selected; NB. nhits is NOT the * number of buffer elements written */ if (nhits < 0) { fprintf(stderr, "gselect buffer overflow\n"); run = FALSE; } else if (nhits >= 1 && buffer[0] == 1 && buffer[1] == PLANET) ringbell(); break; case ESCKEY: run = FALSE; break; } } } gexit(); return 0; }
void plot(char *flags) { int i, j, k; char *t; int32_t x, y; int ra, dec; int m; Point p, pts[10]; Record *r; Rectangle rect, r1; int dx, dy, nogrid, textlevel, nogrey, zenithup; Image *scr; char *name, buf[32]; double v; if(plotopen() < 0) return; nogrid = 0; nogrey = 0; textlevel = 1; dx = 512; dy = 512; zenithup = 0; for(;;){ if(t = alpha(flags, "nogrid")){ nogrid = 1; flags = t; continue; } if((t = alpha(flags, "zenith")) || (t = alpha(flags, "zenithup")) ){ zenithup = 1; flags = t; continue; } if((t = alpha(flags, "notext")) || (t = alpha(flags, "nolabel")) ){ textlevel = 0; flags = t; continue; } if((t = alpha(flags, "alltext")) || (t = alpha(flags, "alllabel")) ){ textlevel = 2; flags = t; continue; } if(t = alpha(flags, "dx")){ dx = strtol(t, &t, 0); if(dx < 100){ fprint(2, "dx %d too small (min 100) in plot\n", dx); return; } flags = skipbl(t); continue; } if(t = alpha(flags, "dy")){ dy = strtol(t, &t, 0); if(dy < 100){ fprint(2, "dy %d too small (min 100) in plot\n", dy); return; } flags = skipbl(t); continue; } if((t = alpha(flags, "nogrey")) || (t = alpha(flags, "nogray"))){ nogrey = 1; flags = skipbl(t); continue; } if(*flags){ fprint(2, "syntax error in plot\n"); return; } break; } flatten(); folded = 0; if(bbox(0, 0, 1) < 0) return; if(ramax-ramin<100 || decmax-decmin<100){ fprint(2, "plot too small\n"); return; } scr = allocimage(display, Rect(0, 0, dx, dy), RGB24, 0, DBlack); if(scr == nil){ fprint(2, "can't allocate image: %r\n"); return; } rect = scr->r; rect.min.x += 16; rect = insetrect(rect, 40); if(setmap(ramin, ramax, decmin, decmax, rect, zenithup) < 0){ fprint(2, "can't set up map coordinates\n"); return; } if(!nogrid){ for(x=ramin; ; ){ for(j=0; j<nelem(pts); j++){ /* use double to avoid overflow */ v = (double)j / (double)(nelem(pts)-1); v = decmin + v*(decmax-decmin); pts[j] = map(x, v); } bezspline(scr, pts, nelem(pts), Endsquare, Endsquare, 0, GREY, ZP); ra = x; if(folded){ ra -= 180*c; if(ra < 0) ra += 360*c; } p = pts[0]; p.x -= stringwidth(font, hm5(angle(ra)))/2; string(scr, p, GREY, ZP, font, hm5(angle(ra))); p = pts[nelem(pts)-1]; p.x -= stringwidth(font, hm5(angle(ra)))/2; p.y -= font->height; string(scr, p, GREY, ZP, font, hm5(angle(ra))); if(x == ramax) break; x += gridra(mapdec); if(x > ramax) x = ramax; } for(y=decmin; y<=decmax; y+=c){ for(j=0; j<nelem(pts); j++){ /* use double to avoid overflow */ v = (double)j / (double)(nelem(pts)-1); v = ramin + v*(ramax-ramin); pts[j] = map(v, y); } bezspline(scr, pts, nelem(pts), Endsquare, Endsquare, 0, GREY, ZP); p = pts[0]; p.x += 3; p.y -= font->height/2; string(scr, p, GREY, ZP, font, deg(angle(y))); p = pts[nelem(pts)-1]; p.x -= 3+stringwidth(font, deg(angle(y))); p.y -= font->height/2; string(scr, p, GREY, ZP, font, deg(angle(y))); } } /* reorder to get planets in front of stars */ tolast(nil); tolast("moon"); /* moon is in front of everything... */ tolast("shadow"); /* ... except the shadow */ for(i=0,r=rec; i<nrec; i++,r++){ dec = r->ngc.dec; ra = r->ngc.ra; if(folded){ ra -= 180*c; if(ra < 0) ra += 360*c; } if(textlevel){ name = nameof(r); if(name==nil && textlevel>1 && r->type==SAO){ snprint(buf, sizeof buf, "SAO%ld", r->index); name = buf; } if(name) drawname(scr, nogrey? display->white : alphagrey, name, ra, dec); } if(r->type == Planet){ drawplanet(scr, &r->planet, map(ra, dec)); continue; } if(r->type == SAO){ m = r->sao.mag; if(m == UNKNOWNMAG) m = r->sao.mpg; if(m == UNKNOWNMAG) continue; m = dsize(m); if(m < 3) fillellipse(scr, map(ra, dec), m, m, nogrey? display->white : lightgrey, ZP); else{ ellipse(scr, map(ra, dec), m+1, m+1, 0, display->black, ZP); fillellipse(scr, map(ra, dec), m, m, display->white, ZP); } continue; } if(r->type == Abell){ ellipse(scr, addpt(map(ra, dec), Pt(-3, 2)), 2, 1, 0, lightblue, ZP); ellipse(scr, addpt(map(ra, dec), Pt(3, 2)), 2, 1, 0, lightblue, ZP); ellipse(scr, addpt(map(ra, dec), Pt(0, -2)), 1, 2, 0, lightblue, ZP); continue; } switch(r->ngc.type){ case Galaxy: j = npixels(r->ngc.diam); if(j < 4) j = 4; if(j > 10) k = j/3; else k = j/2; ellipse(scr, map(ra, dec), j, k, 0, lightblue, ZP); break; case PlanetaryN: p = map(ra, dec); j = npixels(r->ngc.diam); if(j < 3) j = 3; ellipse(scr, p, j, j, 0, green, ZP); line(scr, Pt(p.x, p.y+j+1), Pt(p.x, p.y+j+4), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x, p.y-(j+1)), Pt(p.x, p.y-(j+4)), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x+j+1, p.y), Pt(p.x+j+4, p.y), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x-(j+1), p.y), Pt(p.x-(j+4), p.y), Endsquare, Endsquare, 0, green, ZP); break; case DiffuseN: case NebularCl: p = map(ra, dec); j = npixels(r->ngc.diam); if(j < 4) j = 4; r1.min = Pt(p.x-j, p.y-j); r1.max = Pt(p.x+j, p.y+j); if(r->ngc.type != DiffuseN) draw(scr, r1, ocstipple, ocstipple, ZP); line(scr, Pt(p.x-j, p.y-j), Pt(p.x+j, p.y-j), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x-j, p.y+j), Pt(p.x+j, p.y+j), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x-j, p.y-j), Pt(p.x-j, p.y+j), Endsquare, Endsquare, 0, green, ZP); line(scr, Pt(p.x+j, p.y-j), Pt(p.x+j, p.y+j), Endsquare, Endsquare, 0, green, ZP); break; case OpenCl: p = map(ra, dec); j = npixels(r->ngc.diam); if(j < 4) j = 4; fillellipse(scr, p, j, j, ocstipple, ZP); break; case GlobularCl: j = npixels(r->ngc.diam); if(j < 4) j = 4; p = map(ra, dec); ellipse(scr, p, j, j, 0, lightgrey, ZP); line(scr, Pt(p.x-(j-1), p.y), Pt(p.x+j, p.y), Endsquare, Endsquare, 0, lightgrey, ZP); line(scr, Pt(p.x, p.y-(j-1)), Pt(p.x, p.y+j), Endsquare, Endsquare, 0, lightgrey, ZP); break; } } flushimage(display, 1); displayimage(scr); }