(old)C

#include “stdio.h”
#include “math.h”
#define UPR ‘q’   // here ro define the key.
#define UPL 3
#define DOWNR 5
#define DOWNL 7
#define UP_CURSOR 9
#define DOWN_CURSOR 4
#define swap(a,b) t=a;a=b;b=t;

int main(int argc,char* argv[])
{
if(argc != 5 || strcmp(argv[1],”read_lines”))
{
printf(“error in command !! \n should be: %s read_lines points_file serial_out_file distanse_between_nails\n”,argv[0]);
return -1;
}
FILE* f = fopen(argv[2],”r”);
FILE* fout = fopen(argv[3],”w”);
double nail_dis = atoi(argv[4]);
int last_rl = 0,last_rr = 0;
while(!feof(f))
{
int x1,y1,x2,y2,t;
int num = fscanf(f,”%d %d %d %d\n”,&x1,&y1,&x2,&y2);
if(num != 4)
{
printf(“error in line cordinates format !! should be lines of :x1 y1 x2 y2\n”);
return -1;
}
int r_start = sqrt(x1*x1 + y1*y1);
int r_end = sqrt(x2*x2 + y2*y2);
if(r_start > r_end)
{
swap(r_start,r_end);
swap(x1,x2);
swap(y1,y2);
}
fprintf(fout,”%d”,UP_CURSOR);
double l_x = x1,l_y = y1;
for(int r = r_start;r < r_end; ++r)
{
double dx = x2 – x1;
double dy = y2 – y1;
double dr = sqrt(dx*dx + dy*dy);
double D = x1*y2 – x2*y1;

double cross_x1 = (D*dy + dx*sqrt(r*r*dr*dr – D*D))/(dr*dr);
double cross_y1 = y1 + dy*(cross_x1 – x1)/dx;

double cross_x2 = (D*dy – dx*sqrt(r*r*dr*dr – D*D))/(dr*dr);
double cross_y2 = y1 + dy*(cross_x2 – x1)/dx;

if((cross_x1 – l_x)*(cross_x1 – l_x) + (cross_y1 – l_y)*(cross_y1 – l_y) <
(cross_x2 – l_x)*(cross_x2 – l_x) + (cross_y2 – l_y)*(cross_y2 – l_y))
l_x = cross_x1,l_y = cross_y1;
else
l_x = cross_x2,l_y = cross_y2;

int rl = (int)sqrt(l_x*l_x + l_y*l_y);
int rr = (int)sqrt((l_x – nail_dis)*(l_x – nail_dis) + l_y*l_y);
for(;last_rl < rl;++last_rl)
fprintf(fout,”%d”,UPL);
for(;last_rl > rl;–last_rl)
fprintf(fout,”%d”,DOWNL);

for(;last_rr < rr;++last_rr)
fprintf(fout,”%d”,UPR);
for(;last_rr > rr;–last_rr)
fprintf(fout,”%d”,DOWNR);
if(r == r_start)
fprintf(fout,”%d”,DOWN_CURSOR);
}
}
return 0;
}

Leave a Reply

Your email address will not be published.