TL;DR

On with TASK #2 from the Perl Weekly Challenge #101. Enjoy!

# The challenge

You are given three points in the plane, as a list of six co-ordinates: A=(x1,y1)', B=(x2,y2) and C=(x3,y3). Write a script to find out if the triangle formed by the given three co-ordinates contain origin (0,0). Print 1 if found otherwise

# The questions

A few questions…

• is a point on the edge (or even a vertex) considered to be inside?
• the examples seems to imply that it is
• should we favour an integers-based solution?
• if not, what should our tolerance be?
• is inside a synonym for in the part of the plane delimited by the three segments and with a finite area?
• well, assume yes.

# The solution

I was thinking about a solution involving the vectors from the origin to the three points and how they revolve around the origin itself, then I thought that reinventing the wheel so late is moot and there’s surely some computational geometry article somewhere out there, just to surrender to the power of CPAN.

So… Math::Polygon.

First of all, it does indeed allow to solve the problem quite easily:

sub origin_containing_triangle ($A,$B, $C) { Math::Polygon->new($A, $B,$C, $A)->contains([0, 0]) ? 1 : 0; }  Then, it’s by Mark Overmeer, which I consider a warranty in terms of correctness and accuracy. So… by all means I’ll stick to this solution! #!/usr/bin/env perl use 5.024; use warnings; use experimental qw< postderef signatures >; no warnings qw< experimental::postderef experimental::signatures >; use Math::Polygon; sub origin_containing_triangle ($A, $B,$C) {
Math::Polygon->new($A,$B, $C,$A)->contains([0, 0]) ? 1 : 0;
}

my @pts;
push @pts, [splice @ARGV, 0, 2] while @ARGV;
say origin_containing_triangle(@pts[0..2]);
`

Stay safe!
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7083688378334045, "perplexity": 2429.724360847861}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-25/segments/1623488556133.92/warc/CC-MAIN-20210624141035-20210624171035-00474.warc.gz"}