/******************************************************************* * 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 top(int triangle[]); /** Global Variables ***********************************************/ // There will be NO global variables /******************************************************************* * Function: void main(void) ********************************************************************/ #pragma code void main (void) { int triangle[] = {0, 0 , 3, 4 , 6, 0}; int sampleDistance, resultPerimeter, resultCenterX, resultTop; 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); resultTop = top(triangle); printf("The top of the triangle is %d\n", resultTop); 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[]) { int total=0; //total = triangle[0] + triangle[2] + triangle[4]; int i; for (i=0 ; i<=4 ; i=i+2) total += triangle[i]; return total/3; } /******************************************************************* * Function: top * Input Variables: triangle An array of 6 int variables [x1,y1,x2,y2,x3,y3] Example: int y1_element = triangle[1]; * Output Return: The highest y value of the triangle * Overview: Compares the y values of the triangle and returns the value of the highest y value ********************************************************************/ int top(int triangle[]) { int largest = triangle[1]; if (largest < triangle[3]) { largest = triangle[3]; } if (largest < triangle[5]) { largest = triangle[5]; } return largest; }