How to resolve the algorithm Find if a point is within a triangle step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find if a point is within a triangle step by step in the Ada programming language

Table of Contents

Problem Statement

Find if a point is within a triangle.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find if a point is within a triangle step by step in the Ada programming language

Source code in the ada programming language

-- triangle.ads
generic
	type Dimension is private;
	Zero, Two: Dimension;
	with function "*"(Left, Right: in Dimension) return Dimension is <>;
	with function "/"(Left, Right: in Dimension) return Dimension is <>;
	with function "+"(Left, Right: in Dimension) return Dimension is <>;
	with function "-"(Left, Right: in Dimension) return Dimension is <>;
	with function ">"(Left, Right: in Dimension) return Boolean is <>;
	with function "="(Left, Right: in Dimension) return Boolean is <>;
	with function Image(D: in Dimension) return String is <>;
package Triangle is

	type Point is record
		X: Dimension;
		Y: Dimension;
	end record;

	type Triangle_T is record
		A,B,C: Point;
	end record;

	function Area(T: in Triangle_T) return Dimension;

	function IsPointInTriangle(P: Point; T: Triangle_T) return Boolean;

	function Image(P: Point) return String is
		("(X="&Image(P.X)&", Y="&Image(P.Y)&")")
		with Inline;

	function Image(T: Triangle_T) return String is
		("(A="&Image(T.A)&", B="&Image(T.B)&", C="&Image(T.C)&")")
		with Inline;
end;


-- triangle.adb

package body Triangle is
	function Area(T: in Triangle_T) return Dimension
	is
		tmp: Dimension;
	begin
		tmp:=((T.B.X*T.C.Y-T.C.X*T.B.Y)-(T.A.X*T.C.Y-T.C.X*T.A.Y)+(T.A.X*T.B.Y-T.B.X*T.A.Y))/Two;
		if tmp>Zero then
			return tmp;
		else
			return Zero-tmp;
		end if;
	end Area;

	function IsPointInTriangle(P: Point; T: Triangle_T) return Boolean
	is
	begin
		return Area(T)=Area((T.A,T.B,P))+Area((T.A,P,T.C))+Area((P,T.B,T.C));
	end IsPointInTriangle;
end;


-- test_triangle.adb
with Ada.Text_IO;
use Ada.Text_IO;
with Triangle;

procedure test_triangle
is
	package affine_tri is new Triangle(Dimension=>Integer, Zero=>0,Two=>2, Image=>Integer'Image);
	use affine_tri;
	tri1: Triangle_T:=((1,0),(2,0),(0,2));
	tri2: Triangle_T:=((-1,0),(-1,-1),(2,2));
	origin: Point:=(0,0);
begin
	Put_Line("IsPointInTriangle("&Image(origin)&", "&Image(tri1)&") yields "&IsPointInTriangle(origin,tri1)'Image);
	Put_Line("IsPointInTriangle("&Image(origin)&", "&Image(tri2)&") yields "&IsPointInTriangle(origin,tri2)'Image);
end test_triangle;


  

You may also check:How to resolve the algorithm Associative array/Merging step by step in the Swift programming language
You may also check:How to resolve the algorithm Test a function step by step in the Scala programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Delphi programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Toka programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Quackery programming language