Created
January 5, 2012 03:54
-
-
Save andrewrcollins/1563630 to your computer and use it in GitHub Desktop.
#TJHSST ~ Mouse-Driven Triangle Input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Triangle Input | |
| */ | |
| #include <graphics.h> | |
| #include <math.h> | |
| #include <string.h> | |
| #include "mouse.h" | |
| struct point { | |
| long int x,y; | |
| }; | |
| #define sqr(x) ((x)*(x)) | |
| double getarea(double,double,double); | |
| main() | |
| { | |
| int gdriver,gmode; | |
| double px,py,pz,dpx,dpy,dpz,d01,d12,d20,areat; | |
| int x,y,lastx=(-1),lasty=(-1),cnt; | |
| char ostrx[20],ostry[20],ostrz[20],strx[20],stry[20],strz[20]; | |
| struct point origs[3]; | |
| origs[0].x=20; | |
| origs[0].y=180; | |
| origs[1].x=2; | |
| origs[1].y=20; | |
| origs[2].x=280; | |
| origs[2].y=180; | |
| d01=sqrt((double)(sqr(origs[0].x-origs[1].x)+sqr(origs[0].y-origs[1].y))); | |
| d12=sqrt((double)(sqr(origs[1].x-origs[2].x)+sqr(origs[1].y-origs[2].y))); | |
| d20=sqrt((double)(sqr(origs[2].x-origs[0].x)+sqr(origs[2].y-origs[0].y))); | |
| areat=getarea(d01,d12,d20); | |
| gdriver=CGA; | |
| gmode=CGA; | |
| initgraph(&gdriver,&gmode,""); | |
| setcolor(1); | |
| for(cnt=0;cnt<3;cnt++) | |
| line(origs[cnt].x,origs[cnt].y,origs[(cnt+1)%3].x,origs[(cnt+1)%3].y); | |
| curson(); | |
| while(!getbutton(MOUSE2)) { | |
| if(getbutton(MOUSE1)) { | |
| x=getvaluator(MOUSEX)/2; | |
| y=getvaluator(MOUSEY); | |
| if(lastx!=x||lasty!=y) { | |
| dpx=sqrt((double)(sqr(x-origs[0].x)+sqr(y-origs[0].y))); | |
| dpy=sqfrt((double)(sqr(x-origs[1].x)+sqr(y-origs[1].y))); | |
| dpz=sqrt((double)(sqr(x-origs[2].x)+sqr(y-origs[2].y))); | |
| px=getarea(dpy,dpz,d12); | |
| py=getarea(dpx,dpz,d20); | |
| pz=getarea(dpx,dpy,d01); | |
| px=px/areat; | |
| py=py/areat; | |
| pz=pz/areat; | |
| setcolor(0); | |
| cursoff(); | |
| for(cnt=0;cnt<3;cnt++) | |
| line(lastx,lasty,origs[cnt].x,origs[cnt].y); | |
| outtextxy(280,10,ostrx); | |
| outtextxy(280,30,ostry); | |
| outtextxy(280,50,ostrz); | |
| curson(); | |
| sprintf(strx,"%5.4lf",px); | |
| sprintf(stry,"%5.4lf",py); | |
| sprintf(strz,"%5.4lf",pz); | |
| cursoff(); | |
| setcolor(2); | |
| for(cnt=0;cnt<3;cnt++) | |
| line(x,y,origs[cnt].x,origs[cnt].y); | |
| setcolor(1); | |
| for(cnt=0;cnt<3;cnt++) | |
| line(origs[cnt].x,origs[cnt].y,origs[(cnt+1)%3].x,origs[(cnt+1)%3].y); | |
| outtextxy(280,10,strx); | |
| outtextxy(280,30,stry); | |
| outtextxy(280,50,strz); | |
| curson(); | |
| strcpy(ostrx,strx); | |
| strcpy(ostry,stry); | |
| strcpy(ostrz,strz); | |
| lastx=x; | |
| lasty=y; | |
| } | |
| } | |
| } | |
| cursoff(); | |
| closegraph(); | |
| } | |
| double getarea(a,b,c) | |
| double a,b,c; | |
| { | |
| double s,area; | |
| s=(a+b+c)/2; | |
| area=s*(s-a)*(s-b)*(s-c); | |
| area=sqrt(area); | |
| return area; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A C program found on an old 5.25 floppy disk from when I was a nerd at Thomas Jefferson High School for Science and Technology between 1988 and 1992.
http://en.wikipedia.org/wiki/Ternary_plot
http://en.wikipedia.org/wiki/Phase_diagram
Anyone can do whatever they'd like to with this program--if anything.
I remain a three-sided nerd.