static void merge_dirty_rects(struct drm_clip_rect *rects, int *count) { int a, b; for (a = 0; a < *count - 1; ++a) { for (b = a + 1; b < *count;) { /* collapse to bounding rect if it is fewer pixels */ const int area_a = rect_area(&rects[a]); const int area_b = rect_area(&rects[b]); struct drm_clip_rect bounding_rect = rects[a]; expand_rect(&bounding_rect, &rects[b]); if (rect_area(&bounding_rect) <= area_a + area_b) { rects[a] = bounding_rect; rects[b] = rects[*count - 1]; /* repass */ b = a + 1; --*count; } else { ++b; } } } }
double trap_area(double short_side, double long_side, double height) { //caculate the area of a trapezoid //you can store the result of a function in a variable //to do so just assign a variable to a function that returns something double r_area = rect_area(short_side, height); //the area of the center square double tri_base = (long_side - short_side) / 2.0; double tri_areas = tri_area(tri_base, height) * 2;//the area of the triable parts double t_area = r_area + tri_areas; return t_area; }
double square_area(double length) { //calculate the area of a square //to call a function write its name(first argument, second argument, ...) //when you call a function you immediately jump to that function and execute the code that //is in that function. After that function returns you come back to where you called it //and proceed from where you left off //a function can call other functions //it is perfectly ok return rect_area(length, length); }
//functions don't always have to return a value or accept arguments //if a function doesn't return a value you should place void in front of it void do_rect() { double height, width, area; printf("Please enter the width of the rectangle: "); scanf("%lf", &width); printf("Please enter the height of the rectangle: "); scanf("%lf", &height); area = rect_area(width, height); printf("The area of a rectangle with height %.2lf and width %.2lf is %.2lf\n", height, width, area); //if a function doesn't return anything you don't need a return staement }//do_rect
int main(int argc, char *argv[]) { struct rectangle rect = get_rect(); print_rect(rect); printf("The area of this rectangle: %d\n", rect_area(rect)); printf("Enter a point to check if in the rectangle:"); struct point po = get_point(); if (point_in_rect(po, rect)) { printf("The point is in the rectangle\n"); } else { printf("The point is not in the rectangle\n"); } return 0; }
double tri_area(double base, double height) { //calculate the area of a triangle return rect_area(base, height) / 2.0; }