2. Write a float method triangle() that computes the area of a triangle using its two formal parameters h and w, where h is the height and w is the length of the bases of the triangle.
3. Write a float method rectangle() that computes and returns the area of a rectangle using its two float formal parameters h and w, where h is the height and w is the width of the rectangle.
4. The formula for a line is normally given as y= mx +b. Write a method Line() that expects three float parameters, a slop m, a y-intercept b, and an x-coordinate x. the method computes the y-coordinate associated with the line specified by m and b at x coordinate.
5. Write a method Intersect() with four float parameters m1.b1.m2,b2. The parameters come conceptually in two pairs. The first pair contains the coefficients describing one line; the second pair contains coefficients describing a second line. The method returns I
if the two lines intersect. Otherwise, it should return 0;
// Compute the area of a triangle with a height h and a length of a base w
float triangle(float h, float w) {
return h*w/2;
}
// Compute the area of a rectangle with a height h and a length of a base w
float rectangle(float h, float w) {
return h*w/2;
}
// Compute the y-coordinate associated with line y = mx + b
float line(float m, float b, float x) {
return m*x + b;
}
// Return 1 if lines m1*x+b1 and m2*x + b2 intersect ant 0 otherwise
int intesect(float m1, float b1, float m2, float b2) {
if (m1 != m2 || b1 == b2) {
return 1;
}
return 0;
}
Comments
Leave a comment