c++ - Structured Data: Nesting Triangles using structs and calculating -
problem description:write code of function takes triangle input , returns area. expected figure out how access data, understanding abstract data types defined.you may use method compute area vertices
you required write function:
function name : cal_area
this function has 1 parameter
triangle atriangle: contains 3 vertices of triangle in x-y plane. expected figure out how access data. function should following
it should return area of triangle "atriangle" float.
well question need fill following
//-----include required headers here----- #include <iostream.h> #include <cmath> //-----end of headers----- //-----don't change/delete structs----- struct vertex{ float x; float y; }; struct triangle{ vertex vertices[3]; }; struct linesegment { point endpoint1; point endpoint2; }; //-----structs end here //-----add new functions here(if any)----- double eval (linesegment line, point p) { point p_a = p - line.endpoint1; point l_dir = line.endpoint2 - line.endpoint1; return p_a.x * l_dir.y - p_a.y * l_dir.x; } void maketrianglecounterclockwise(triangle & t) { linesegment ab = {t.vertices[1], t.vertices[0]}; if ( eval(ab,t.vertices[2]) < 0.0) { swap(t.vertices[0],t.vertices[1]); } } //-----new functions end here----- float cal_area(triangle atriangle) { //write solution below line return (-1); //dont write below line }
i tried write fuction went wrong pls help
i think you’re trying this:
float cal_area(triangle atriangle) { float area; area = (atriangle.vertices[1].x - atriangle.vertices[0].x) * (atriangle.vertices[2].y - atriangle.vertices[1].y) - (atriangle.vertices[1].y - atriangle.vertices[0].y) * (atriangle.vertices[2].x - atriangle.vertices[1].x); if (area < 0.0f) { return -area / 2.0f; } else { return area / 2.0f; } }
Comments
Post a Comment