/******************************************************************* * FileName: triangles.c * Processor: PIC18F4520 * Compiler: MPLAB C18 v.3.06 * * This file uses functions to calculate triangle parameters * and determines the top of the triangle * * * Creation and Revisions: * Author Date Comments * (Your name here) ********************************************************************/ /** Header Files ***************************************************/ #include #include #include /** Configuration Bits *********************************************/ #pragma config OSC = EC // EC = External 4MHz Crystal for PICDEM board only #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF /** Define Constants Here ******************************************/ /** Local Function Prototypes **************************************/ int distance(int x1, int y1, int x2, int y2); int perimeter(int triangle[]); int centerX(int triangle[]); int middle(int triangle[]); /** Global Variables ***********************************************/ // There will be NO global variables /******************************************************************* * Function: void main(void) ********************************************************************/ #pragma code void main (void) { int triangle[] = {0, 0 , 4, 4 , 12, 0}; int sampleDistance, resultPerimeter, resultCenterX, resultMiddle; sampleDistance = distance(0, 0, 5, 12); printf("The sampleDistance (as int) = %d\n", sampleDistance); resultPerimeter = perimeter(triangle); printf("The perimeter (as int) = %d\n", resultPerimeter); resultCenterX = centerX(triangle); printf("The x center of the triangle (as int) = %d\n", resultCenterX); resultMiddle = middle(triangle); printf("The middle x-coordinate of the triangle is %d\n", resultMiddle); while (1) { // This area loops forever } } /******************************************************************* * Additional Helper Functions ********************************************************************/ /******************************************************************* * Function: distance * Input Variables: x1 - Point 1 x value y1 - Point 1 y value x2 - Point 2 y value y2 - Point 2 y value * Output Return: The distance between the points (truncated to an int) * Overview: Determines the distance between Point 1 and Point 2 ********************************************************************/ int distance(int x1, int y1, int x2, int y2) { float resultFloat, deltaXFloat, deltaYFloat; int result, deltaX, deltaY; deltaX = x2 - x1; deltaY = y2 - y1; deltaXFloat = (float) deltaX; deltaYFloat = (float) deltaY; resultFloat = sqrt(deltaXFloat*deltaXFloat + deltaYFloat*deltaYFloat); result = (int) (resultFloat+.000001); return result; } /******************************************************************* * Function: perimeter * Input Variables: triangle An array of 6 int variables [x1,y1,x2,y2,x3,y3] Example: int x1_element = triangle[0]; * Output Return: The perimeter of the triangle (truncated to an int) * Overview: Determines the perimeter of the triangle by adding up the lenth of each side ********************************************************************/ int perimeter(int triangle[]) { int a, b, c; a = distance(triangle[0], triangle[1], triangle[2], triangle[3]); b = distance(triangle[0], triangle[1], triangle[4], triangle[5]); c = distance(triangle[2], triangle[3], triangle[4], triangle[5]); return a + b + c; } /******************************************************************* * Function: centerX * Input Variables: triangle An array of 6 int variables [x1,y1,x2,y2,x3,y3] Example: int x1_element = triangle[0]; * Output Return: The average of the x values of the triangle (truncated as an int) * Overview: Determines the x value of the centroid of the triangle, which is an average of the x values ********************************************************************/ int centerX(int triangle[]) { // Note: You MUST use a for loop when accumulating the total for full credit int i, total = 0; for (i = 0; i <= 4; i+=2) { // x's are every other total += triangle[i]; } return total/3; } /******************************************************************* * Function: middle * Input Variables: triangle An array of 6 int variables [x1,y1,x2,y2,x3,y3] Example: int x1_element = triangle[0]; * Output Return: The middle x value of the triangle * Overview: Compares the x values of the triangle and returns the value of the one between the others ********************************************************************/ int middle(int triangle[]) { int x1 = triangle[0]; int x2 = triangle[2]; int x3 = triangle[4]; // Two ways for x2 to be middle (so use OR) if (x1 <= x2 && x2 <= x3 || x1 >= x2 && x2 >= x3) return x2; // Same for x1 and x3... if (x2 <= x1 && x1 <= x3 || x2 >= x1 && x1 >= x3) return x1; if (x1 <= x3 && x3 <= x2 || x1 >= x3 && x3 >= x2) return x3; }