Problem with downloading data - vertices of square

43 Views Asked by At

I would like to write a program in which you give two vertices of the square and the program finds the other two.

for example:

input:

2 3 4 5

output

(3,6) (0,5)

But I have a problem when it reads data from the program everything works fine

int main(void)
{

    rationalNumber *z,e,f,g,h;

    point *x, a, b, c, d;

    e.l = 2; e.m = 1; f.l=3; f.m=1; g.l = 4; g.m = 1; h.l=5; h.m=1;

    a.x = e;
    a.y = f;
    b.x = g;
    b.y = h;
    output_point(D_2(z,a, b));


return 0;
}

but when reads data from a user, I gets this message

"Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"

In this place:

if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;

All code:

#include<stdio.h>
#include<stdlib.h>

typedef struct rn{
    int n;   /**numerator**/
    unsigned d;    /**denomirator**/
} rationalNumber;

int gcd(int,int);

void output(rationalNumber);
void input(rationalNumber *);
void Minus(rationalNumber*, rationalNumber, rationalNumber);
void Sum( rationalNumber *, rationalNumber, rationalNumber);
void Multiplication(rationalNumber *, rationalNumber, rationalNumber);
void Reciprocal( rationalNumber *);

typedef struct dot{
    rationalNumber x;
    rationalNumber y;
} point;

void load_point(point *);
void output_point(point);
point C_1(rationalNumber *, point, point);
point D_1(rationalNumber *, point, point);
point C_2(rationalNumber *, point, point);
point D_2(rationalNumber *, point, point);


int main(void)
{

    rationalNumber *z;
    point *a,b,c;

    load_point(a);
    b = *a;
    load_point(a);
    c = *a;

    output_point(D_2(z,b, c));


return 0;
}

int gcd(int a, int b)
{
  if(b!=0)
    return gcd(b,a%b);

       return a;
}


void output(rationalNumber a)
{
    if(a.d == 1)  printf("%d",a.n);
    else printf("%d/%u",a.n, a.d);
}

void input(rationalNumber *a)
{
    int nwd;
    if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;
    else
    {
        nwd = abs(gcd(a->n, a->d));
        a->n = a->n/nwd;
        a->d = a->d/nwd;
    }


}

void Minus(rationalNumber *a, rationalNumber b, rationalNumber c)
{
   int nwd;
      if(b.d == c.d)
      {
          a->n = b.n - c.n;
          a->d = b.d;
          nwd = abs(gcd(a->n, a->d));
          a->n = a->n/nwd;
          a->d = a->d/nwd;

      }else
      {
          a->n = b.n*c.d - c.n*b.d;
          a->d = b.d * c.d;
          nwd = abs(gcd(a->n, a->d));
          a->n = a->n/nwd;
          a->d = a->d/nwd;

      }
}

rationalNumber minus( rationalNumber a)
{
    return a;
}

void Sum( rationalNumber *a, rationalNumber b, rationalNumber c)
{
    int nwd;
    if(b.d == c.d)
    {
        a->n = b.n + c.n;
        a->d = b.d;
    }else
    {
        a->n = b.n*c.d + c.n*b.d;
        a->d = b.d * c.d;
        nwd = abs(gcd(a->n, a->d));
        a->n = a->n/nwd;
        a->d = a->d/nwd;

    }

}

void Multiplication( rationalNumber *a, rationalNumber b, rationalNumber c)
{
    int nwd;

    a->n = b.n*c.n;
    a->d = b.d *c.d;

    nwd = abs(gcd(a->n, a->d));
    a->n = a->n/nwd;
    a->d = a->d/nwd;

}


void Reciprocal( rationalNumber *a)
{
    int buffor;

    if(a->n<0)
    {
        buffor = abs(a->n);
        a->n = -(a->d);
        a->d = buffor;
    }
    else if(a->n>0)
    {
        buffor = a->n;
        a->n = a->d;
        a->d = buffor;
    }


}

void load_point(point *a)
{
    input(&a->x);
    input(&a->y);

}

void output_point(point a)
{
    printf("(");
    output(a.x);
    printf(",");
    output(a.y);
    printf(")\n");
}

point C_1(rationalNumber *c_1, point a, point b)
{

    point c;

    Sum(c_1,b.x,a.y);

    Minus(c_1,*c_1, b.y);
    c.x = *c_1;

    Sum(c_1,b.y,b.x);

    Minus(c_1,*c_1, a.x);
    c.y = *c_1;

    return c;
}

point D_1(rationalNumber *d_1, point a, point b)
{
    point d;

    Sum(d_1,a.x,a.y);

    Minus(d_1,*d_1, b.y);
    d.x = *d_1;

    Sum(d_1,a.y,b.x);

    Minus(d_1,*d_1, a.x);
    d.y = *d_1;

    return d;
}

point C_2(rationalNumber *c_1,point a, point b)
{
    point c;

    Minus(c_1,b.x,a.y);

    Sum(c_1,*c_1, b.y);
    c.x = *c_1;

    Minus(c_1,b.y,b.x);

    Sum(c_1,*c_1, a.x);
    c.y = *c_1;

    return c;
}

point D_2(rationalNumber *d_1,point a, point b)
{
    point d;

    Minus(d_1,a.x,a.y);

    Sum(d_1,*d_1, b.y);
    d.x = *d_1;

    Minus(d_1,a.y,b.x);

    Sum(d_1,*d_1, a.x);
    d.y = *d_1;

    return d;
}

I' don't know how I can fix it

1

There are 1 best solutions below

0
On

If you have one side of the square as (a, b), you can immediately have one of it's two normals as (b, -a) (or (-b, a), your problem is ambiguous) and the last point you can get by adding both vectors, so (a + b, b - a) (or (a - b, b + a), depending on which vector you selected first)

The only thing for this to be transportable to any pair of points in the plane is to subtract one of the vectors first to move the figure origin.... once the problem is solved, you can move the whole thing back to the point you had it.

So, assuming you are given the two vectors P = (a, b) and Q = (c, d), the first task is to subtract P = (a, b) from Q = (c, d) to make it to start from the origin, then you get P'(a - a, b - b) = P'(0, 0); Q' = (c - a, d - b), now you generate your normal vector (you exchange coordinates and change the sign to one of them), let's say, R' = (-d + b, c - a), and add both vectors to get S' = (c - a - d + b, c - a + d - b), the opposite diagonally. These are the two points you get. Let's move the whole thing back to the original point:

P' = (0, 0) ==> P = (0 + a, 0 + b) = (a, b) = P
Q' = (c - a, d - b) ==> (c - a + a, d - b + b) = (c, d) = Q
R' = (b - d, c - a) ==> (b - d + a, c - a + b) = R
S' = (c - a - d + b, c - a + d - b) ==> (c - a - d + b + a, c - a + d - b + b) = (c + b - d, c - a + d) = S

So, if you get two points P = (a, b) and Q = (c, d) you have to return R = (a + b - c, -a + b + c) and S = (b + c - d, -a + c + d).

double a, b, c, d;

scan("%g%g%g%g", &a, &b, &c, &d);
printf("Given (%g, %g) and (%g, %g),\n"
       "we got the other two vertices "
       "as (%g, %g) and (%g, %g)\n",
       a, b, c, d,
       a + b - c, b - a + c, 
       b + c - d, c - a + d);