Пример #1
0
//Finds the two points at which the line AB passes through the circle at C.
//The discriminant d can be used to determine whether the line is secant, tangent, or there is no intersection
//d < 0 - no intersection
//d > 0 - secant
//d = 0 - tangent
//Input: radius, center of circle, line defined by AB
struct Point* cIntersect(double r, struct Point c, struct Point a, struct Point b){
   a = sub(a, c);
   b = sub(a, c);
   double dx = b.x-a.x;
   double dy = b.y-a.y;
   double dr = sqrt(dx*dx + dy*dy);
   double D = cross(a, b);
   double d = r*r*dr*dr-D*D;

   double x1 = (D*dy+sgn(dy)*dx*sqrt(r*r*dr*dr-D*D))/(dr*dr);
   double y1 = (-D*dx+sgn(dy)*dy*sqrt(r*r*dr*dr-D*D))/(dr*dr);
   double x2 = (D*dy-sgn(dy)*dx*sqrt(r*r*dr*dr-D*D))/(dr*dr);
   double y2 = (-D*dx-sgn(dy)*dy*sqrt(r*r*dr*dr-D*D))/(dr*dr);

   struct Point i1 = nP(x1, y1);
   struct Point i2 = nP(x2, y2);
   //The two points of intersection
   i1 = add(i1,c);
   i2 = add(i2,c);
   struct Point *ret = malloc(2*sizeof(struct Point));

   ret[0] = i1;
   ret[1] = i2;
   return ret;
}
Пример #2
0
void run_jacobi(const int N, const float error, const int iter,
				long &c_time, long &cuda_time)
{
	linearMatrix A(N, N);
	linearMatrix B(N,1);
	linearMatrix X(N,1);
	prepare_matrices(A, B, X);

	nonParallel nP(A, B, X, error, iter);
	nP.With_Jacobi();


	Parallel P(A, B, X, error, iter);
	P.With_Jacobi();

	linearMatrix C_SOL(N, 1);
	linearMatrix CUDA_SOL(N, 1);

	C_SOL = nP.Get_Solution();
	CUDA_SOL = P.Get_Solution();

	int err = check_solutions(C_SOL, CUDA_SOL);
	if(err >= 0)
	{
		printf("Checked solutions!\n");
		c_time = nP.Get_Time_Used();
		cuda_time = P.Get_Time_Used();
	}
	else
	{
		printf("Dimension errors");
	}
}
Пример #3
0
/*ostring DiagParams::AlignmentConstraint()
{char buffer[200];
 if (((j1.getValue() == j2.getValue()) &&
		(k1.getValue() != k2.getValue())) ||
	  ((j1.getValue() != j2.getValue()) &&
		(k1.getValue() == k2.getValue())))
  sprintf(buffer, "%s","");
 else
  sprintf(buffer, "boundary (%d, %d)- (%d, %d) is not aligned with axis",
		j1.getValue(), k1.getValue(), j2.getValue(), k2.getValue());
 return(ostring(buffer));}

ostring DiagParams::OnGridConstraint()
{char buffer[200];
 if  (Between2(j1.getValue(), 0, GP->getJ()) && Between2(k1.getValue(), 0, GP->getK()) &&
		Between2(j2.getValue(), 0, GP->getJ()) && Between2(k2.getValue(), 0, GP->getK()))
  sprintf(buffer, "%s","");
 else
  sprintf(buffer, "boundary (%d, %d)- (%d, %d) is not contained on grid",
		j1.getValue(), k1.getValue(), j2.getValue(), k2.getValue());
 return(ostring(buffer));}

BOOL DiagParams::HasLength()
{
	if ((j1.getValue() == j2.getValue()) && (k1.getValue() == k2.getValue()))
		return 0;
	else
		return 1;
}

*/
void DiagParams::checkRules()
{
//ruleMessages.removeAll();
 ostring result = AlignmentConstraint();
 if (strlen(result.c_str()) > 0)
	 ruleMessages.add(new ostring(result));
 result = OnGridConstraint();
 if (strlen(result.c_str()) > 0)
	 ruleMessages.add(new ostring(result));
 // Loop thru rules
 oopicListIter<RuleBase> nR(RuleList);
 oopicListIter<BaseParameter> nP(parameterList);
 for (nR.restart(); !nR.Done(); nR++)
 // Loop thru parameters and set parameter values required by rule
  {for (nP.restart(); !nP.Done(); nP++)
	 nR.current()->setRuleVariable(nP.current()->getName(),
	 nP.current()->getValueAsDouble());
 // Check the rule
	ostring result = nR.current()->checkRule();
	if (strlen(result.c_str()) > 0)
	 ruleMessages.add(new ostring(result));}}
Пример #4
0
int main(void)
{
	scanf("%d", &te);
	for(t = 0; t < te; ++ t)
	{
		scanf("%d", &si);
		if(!si)continue;
		if(!re[si - 1])
		{
			re[si - 1] = (char*)malloc((1 + 11 * sil(si)) * sizeof(char));
			do
			{
				for(d = 0; d < si; ++ d)
					re[si - 1][r[si - 1] ++] = da[d];

				re[si - 1][r[si - 1] ++] = '\n';
			}
			while(nP(da, da + si - 1));
		}
		puts(re[si - 1]);
	}

	return 0;
}
Пример #5
0
int main(){
   //Open the input file
   FILE *fin;
   fin = fopen("mice.in", "r");

   int times, k;

   //Scan in the number of ponds to process
   fscanf(fin, "%d\n", &times);

   //Begin processing input
   for(k = 1; k <= times; k++){
      //Read in case data
      double rp, rh, x, y;
      int irp, irh, ix, iy;
      fscanf(fin, "%d %d %d %d", &irp, &irh, &ix, &iy);
      rp = (double)irp;
      rh = (double)irh;
      x = (double)ix;
      y = (double)iy;

      /* The largest rectangle that fits the constraints of the problem, one side being twice the length of the other,
         and fits inside of the circle, will be inscribed in the circle. That is, its four points will all touch the circle's edge and its center
         will be the origin

         We'll assume the short side of the rectangle is length L, and the long side length 2L. Therefore, its area is 2*L^2.

         If we break the rectangle up into 8 right triangles by drawing lines from the origin to its corners and perpendicular lines bisecting its sides,
         we find that one of these triangles has sides L, (1/2)*L, and rp. Using the pythagorean theorem, we find that

         rp^2 = L^2 + (1/4)*L^2.
         rp^2 = (5/4)*L^2
         (4/5)*rp^2 = L^2

         Therefore, using the area we came up with earlier, we can replace L^2 and find that the area of the largest possible rectangle is in fact

         A = 2*((4/5)*rp^2) */

      double L = sqrt(rp*rp*4./5.); //Length of the shorter side
      double area = 2.*L*L; //Area of the original rectangle
      double narea = 0; //Area of the new rectangle

      struct Point o = nP(0, 0); //Point representing the origin
      struct Point holeCenter = nP(x, y); //Point representing the center of the hole

      //Find the two points in the hole at which the line extending from the origin to the hole center intersects with the hole
      struct Point *consider = cIntersect(rh, holeCenter, o, holeCenter);
      struct Point i; //We will place the closer of the two in this struct

      //If the hole is centered on the origin, we will need to choose two arbitrary points on the hole's circumference, as there is no line
      if(x==0&&y==0){
         consider[0] = nP(0,rh);
         consider[1] = nP(0,-rh);
      }

      //Choose the point which is closer to the origin
      if(dis(consider[0], o) < dis(consider[1], o)){
         i = consider[0];
      }else{
         i = consider[1];
      }

      //printf("Considering (%.2f %.2f) and (%.2f %.2f)\n", consider[0].x, consider[0].y, consider[1].x, consider[1].y);

      /*  Once we have this point, we will attempt to calculate the area of the largest rectangle that could be placed adjacent to it.
          Consider the placement of the rectangle. Its top side would have to be tangent to this point, touching at the midpoint to maximize its area.

          As such, imagine extending a line from this point at a 45 degree angle downward, until touching the edge of the pond. This line represents
          the diagonal of the left half of the rectangle. Finding the length of this line will allow us to find the area of the rectangle, using the following
          reasoning:

               The diagonal is of length c.
               It forms two isoscoles triangles.
               a^2 + b^2 = c^2
               In an isoscoles triangle, a = b
               a^2 + a^2 = c^2
               2*a^2 = c^2
               If one of the sides is of length a, the area of the square is a^2. Because the rectangle is composed of two of these squares, the rectangle has area 2*a^2.
               Therefore, A_rectangle = 2*a^2 = c^2

          We know that the angle will be extended at a 45 degree angle, but not much else. We do, however, have the circle to work with. By forming a triangle
          using point i, the origin, and the point on the edge of the hole, we can use the law of sines to solve the triangle for c. The length of one of the
          sides is rp, the length of another side is the distance from point i to the origin, and we have the angle of the line we extended, 45 degrees.

          What we find in solving the triangle is that it comes down to three cases. */

      //Three cases
      if(i.x==0&&i.y==0){
         //Case 1: Point i is (0,0)
         //The solution is rp*rp, as the length of the line is the radius of the circle
         narea = rp*rp;
      }else if(dis(o, holeCenter) > rh){
         //Case 2: The origin is outside of the hole, c is the hypotenuse of the triangle
         //Solve for c with sin(45)/r=sin(tb)/p.d((0,0))=sin(tc)/c
         //Solution is c^2
         narea = solveTriangle(45, rp, dis(i, o));
      }else if(dis(o, holeCenter) < rh){
         //Case 3: The origin is inside of the hole, rp is the hypotenuse
         //Solve for c with sin(135)/r=sin(tb)/p.d((0,0))=sin(tc)/c
         //Solution is c^2
         narea = solveTriangle(135, rp, dis(i, o));
      }

      //printf("Before: %.2f After: %.2f\n\n",area, narea);

      printf("Pond #%d:\n",k);
      //If the new area exceeds the original area, there is space for the original area to exist (and the new area actually goes out of bounds!)
      if(narea > area){
         //ICE CLEAR!!!
         printf("ICE CLEAR!!! %.2f\n\n", area);
      }else{
         //OBSTRUCTION!
         printf("OBSTRUCTION! %.2f\n\n", narea);
      }

      //Free your allocated memory
      free(consider);
   }

   return 0;
}
Пример #6
0
//Add one point to another
struct Point add(struct Point a, struct Point b){
   return nP(a.x+b.x, a.y+b.y);
}
Пример #7
0
//Subtract one point from another
struct Point sub(struct Point a, struct Point b){
   return nP(a.x-b.x, a.y-b.y);
}
Пример #8
0
int testNormalized(FILE * f)
{
     fprintf (f, "\t+++++ Enter testNormalized() +++++\n");
     int ntests=0, res=0;
     Point P(0,0), nP(0,0), eP(0,0);
     //-----------------------------------------------------------------------------
     nP = P.Normalized();
     eP._x = 0;
     eP._y = 0;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     P._x = -0.5;
     P._y = 0;
     nP = P.Normalized();
     eP._x = -1;
     eP._y = 0;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     P._x = 0;
     P._y = -0.5;
     nP = P.Normalized();
     eP._x = 0;
     eP._y = -1;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     P._x = -0.5;
     P._y = 0.5;
     nP = P.Normalized();
     eP._x = -1./sqrt(2);
     eP._y = -eP._x;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     P._x = 1000.432;
     P._y = 0;
     nP = P.Normalized();
     eP._x = 1;
     eP._y = 0;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     P._x = 0;
     P._y = -12345.789;
     nP = P.Normalized();
     eP._x = 0;
     eP._y = -1;
     res += ( nP == eP )?1:0;
     ntests++;
     fprintf (f, "%2d. res=%2d, P(%.2f, %.2f), nP(%.2f, %.2f), eP(%.2f, %.2f)\n", ntests, res, P._x, P._y, nP._x, nP._y, eP._x, eP._y );
     //------------------------------------------------------------------------------
     fprintf (f, "\t+++++ Leave testNormalized() +++++\n\n");
     return (res==ntests)?1:0;
}