docx - csserver

advertisement
Engr 123
2D Sample Problem
Create a 2-d array that has the following data;
double [,] pt = { { 0.0,
{10.2,
{ 9.7,
{-4.5,
{-4.9,
{-9.0,
{ 6.3,
{ 6.5,
{-1.0,
{-6.2,
};
0.0},
2.3},
4.5},
3.2},
-5.7},
2.9},
-9.1},
-1.8},
-4.1},
-2.6}
//
//
//
//
//
//
//
//
//
//
Point
point
point
point
point
point
point
point
point
point
0
1
2
3
4
5
6
7
8
9
Take this data to represent a set of points on a two-dimensional plane. Each set of three points
can form a triangle. Find the triangles with the maximum and the minimum area for this set of
points. You can find the area of a triangle by using the following equations:
s  (d1  d 2  d 3) / 2;
area  s( s  d1)( s  d 2)( s  d 3)
dx  ( x1  x2 ) 2  ( y1  y2 ) 2
Your program should not only print the maximum and minimum areas, it should also print the
point numbers that form the maximum and minimum triangles. Point numbers are given in the
comments above.
The following pages show two solutions to this problem. In the first solution the point array is
declared inside the main program and passed as a parameter to each of the methods. In the
second solution the point array is declared inside the class but not inside the main program.
This allows access to the array from any method in the class.
Solution 1 – The point array is inside the main program and is passed to the methods.
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
namespace TriangleArea
{class Program
{static void Main(string[] args)
{double [,] pt = { { 0.0, 0.0}, // Point 0
{10.2, 2.3}, // point 1
{ 9.7, 4.5}, // point 2
{-4.5, 3.2}, // point 3
{-4.9, -5.7}, // point 4
{-9.0, 2.9}, // point 5
{ 6.3, -9.1}, // point 6
{ 6.5, -1.8}, // point 7
{-1.0, -4.1}, // point 8
{-6.2, -2.6} // point 9
};
int i, j, k;
int iMax, jMax, kMax;
double area, maxArea;
iMax = 0;jMax = 1;kMax = 2;
maxArea = FindArea(pt, iMax, jMax, kMax);
for(i=0;i<8;i++)
{for(j=i+1;j<9;j++)
{for(k=j+1;k<10;k++)
{area = FindArea(pt, i, j, k);
if(maxArea < area)
{maxArea = area;
iMax = i;jMax = j;kMax = k;
}
}
}
}
Console.WriteLine("The largest triangle has an area of {0}", maxArea);
Console.WriteLine("The vertices are at [{0}, {1}], [{2}, {3}], and [{3}, {4}]",
pt[iMax,0], pt[iMax, 1], pt[jMax,0], pt[jMax, 1], pt[kMax,0], pt[kMax, 1]);
}
private static double FindArea(double[,] pt, int i, int j, int k)
{double d1, d2, d3, s;
d1 = FindDistance(pt, i, j);
d2 = FindDistance(pt, j, k);
d3 = FindDistance(pt, i, k);
s = (d1 + d2 + d3)/2;
return(Math.Sqrt(s*(s-d1)*(s-d2)*(s-d3)));
}
private static double FindDistance(double[,] pt, int i, int j)
{double x1, x2, y1, y2;
x1 = pt[i, 0];x2 = pt[j, 0];
y1 = pt[i, 1];y2 = pt[j, 1];
return (Math.Sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)));
}
}
}
Solution 2: The point array is inside the class but not inside the main program. It can be accessed by any
method in the class and does not need to be an parameter.
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
namespace TriangleArea
{class Program
{static double [,] pt = { { 0.0, 0.0}, // Point 0
{10.2, 2.3}, // point 1
{ 9.7, 4.5}, // point 2
{-4.5, 3.2}, // point 3
{-4.9, -5.7}, // point 4
{-9.0, 2.9}, // point 5
{ 6.3, -9.1}, // point 6
{ 6.5, -1.8}, // point 7
{-1.0, -4.1}, // point 8
{-6.2, -2.6} // point 9
};
static void Main(string[] args)
{int i, j, k;
int iMax, jMax, kMax;
double area, maxArea;
iMax = 0;jMax = 1;kMax = 2;
maxArea = FindArea(iMax, jMax, kMax);
for(i=0;i<8;i++)
{for(j=i+1;j<9;j++)
{for(k=j+1;k<10;k++)
{area = FindArea(i, j, k);
if(maxArea < area)
{maxArea = area;
iMax = i;jMax = j;kMax = k;
}
}
}
}
Console.WriteLine("The largest triangle has an area of {0}", maxArea);
Console.WriteLine("The vertices are at [{0}, {1}], [{2}, {3}], and [{3}, {4}]",
pt[iMax,0], pt[iMax, 1], pt[jMax,0], pt[jMax, 1], pt[kMax,0], pt[kMax, 1]);
}
private static double FindArea(int i, int j, int k)
{double d1, d2, d3, s;
d1 = FindDistance(i, j);
d2 = FindDistance(j, k);
d3 = FindDistance(i, k);
s = (d1 + d2 + d3)/2;
return(Math.Sqrt(s*(s-d1)*(s-d2)*(s-d3)));
}
private static double FindDistance(int i, int j)
{double x1, x2, y1, y2;
x1 = pt[i, 0];x2 = pt[j, 0];
y1 = pt[i, 1];y2 = pt[j, 1];
return (Math.Sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)));
}
}
}
Download