### Author Topic: Inside/Outside Triangle (After Problem Set by STxAxTIC)  (Read 3611 times)

0 Members and 1 Guest are viewing this topic.

#### Qwerkey

• Forum Resident
• Posts: 755
##### Inside/Outside Triangle (After Problem Set by STxAxTIC)
« on: June 18, 2020, 10:22:20 am »
STxAxTIC has set a problem to solve for a point being inside or outside of a triangle.  This program (I believe) solves that.  It works by checking whether three created angles add up to 360degrees - see original post:
https://www.qb64.org/forum/index.php?topic=2714.0
When you run the program, first click in the work area three times to define a triangle.  Then as you move about the work area, the display label will show whether the cursor is inside or outside of the triangle.  Click Restart to produce a different triangle.

« Last Edit: June 20, 2020, 12:44:42 pm by Qwerkey »

#### TempodiBasic

• Forum Resident
• Posts: 1792
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #1 on: June 18, 2020, 11:25:04 am »
Cool!
It works well!

A good approach to the problem!
Reading the question posted by STxAxTIC I have thought of using Barycenter or Centroid https://en.wikipedia.org/wiki/Centroid  https://it.wikipedia.org/wiki/Baricentro_(geometria)
I'll try to do some code about it this evening.
Programming isn't difficult, only it's  consuming time and coffee

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #2 on: June 18, 2020, 12:56:00 pm »
Oh dear!  Just found that it gets errors near the corners: thought that I had checked it out, obviously not thoroughly enough.  I hope that I've time to look tomorrow.  The trial code from which this is dervied seems OK at the corners.  => Now corrected.
« Last Edit: June 21, 2020, 08:19:37 am by Qwerkey »

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 1091
• he lives
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #3 on: June 18, 2020, 01:22:29 pm »
Hmmmmm owell... I'm sure you'll find the typo or whatever caused it. The idea seems solid enough for an explicit trig approach. If in doubt though check your angles against what a cross product will give you? Might prevent weirdness.
You're not done when it works, you're done when it's right.

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #4 on: June 18, 2020, 02:49:48 pm »
I think that I know where the error is: a plotting offset that I haven't taken into account - twit!  Tomorrow's little job.

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #5 on: June 19, 2020, 04:12:38 am »
The error was indeed the plotting offset: the drawing is done on an Inform PictureBox, which is placed at (10,10) in the window.  How did I manage not to spot this offset?? Anyway, program corrected and replaced at the beginning of this post.  My apologies.  Let me know if you find any remaining errors.

In this program, the Y- dimension is increasing as vertical position goes downwards (standard screen convention).  Originally I had intended to correct all the trig calculations as if Y- increases in the upward direction.  But as the geometry is mirror symmetric in Y-, this has not seemed necessary (but highlights the lack of rigorousness in the coder).

#### bplus

• Global Moderator
• Forum Resident
• Posts: 8053
• b = b + ...
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #6 on: June 19, 2020, 05:19:07 pm »
Nice program if I need refresher of InForm drawing I will use this.

Tiny typo
Code: QB64: [Select]
1.         CASE RestartBT
2.             ClickCount% = 0
3.             BeginDraw PictureBox1
4.             COLOR _RGB32(0, 0, 0), _RGB32(255, 255, 255)
5.             EndDraw PictureBox1
6.             Caption(X1LB) = ""
7.             Caption(Y1LB) = ""
8.             Caption(X2LB) = ""
9.             Caption(Y2LB) = ""
10.             Caption(X3LB) = ""
11.             Caption(Y3LB) = ""
12.             Control(InsideOutsideLB).Hidden = True
13.             Caption(Click1LB) = "Clcik To Set"  ' <<<<<<<<
14.             Caption(Click2LB) = "Vertex 1"
15.

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #7 on: June 20, 2020, 03:31:23 am »
@bplus Thanks for your kind, as ever, comments.  Thanks also for finding another error!  As you know I am normally very slow but I was in a hurry to win @STxAxTIC 's prize of an all-expenses-paid trip to the Great Pyramid of Giza, and it is something of a pity that here in lock-down the prize has been downgraded to a trip to my back garden.  I shall at any rate enjoy weeding my strawberry patch and pruning my peach tree.

#### bplus

• Global Moderator
• Forum Resident
• Posts: 8053
• b = b + ...
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #8 on: June 20, 2020, 11:56:52 am »
@bplus Thanks for your kind, as ever, comments.  Thanks also for finding another error!  As you know I am normally very slow but I was in a hurry to win @STxAxTIC 's prize of an all-expenses-paid trip to the Great Pyramid of Giza, and it is something of a pity that here in lock-down the prize has been downgraded to a trip to my back garden.  I shall at any rate enjoy weeding my strawberry patch and pruning my peach tree.

Perhaps we can try 3D pyramids on screen and beat the Egyptians Ancient Aliens at their own game.

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #9 on: June 20, 2020, 12:45:38 pm »
Feeling a need to correct the slight typo which @bplus found, I have decided to make a final, final version.  Here the cursor position values are displayed as well as the three vertices (as this ought to be if we're creating program to compare the cursor position with the triangle).  And I have also changed the calculations to use completely correct position comparisons, rather than the slight 'bodge' which I implemented to correct for the original plotting offset errors.  It is a neater piece of coding, but you will hardly want to examine it if you've already tried the program.

#### FellippeHeitor

• Guest
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #10 on: June 20, 2020, 11:46:48 pm »
Perfect combo of InForm controls and drawing commands to PictureBox, @Qwerkey!

PS: If you make the .Stretch property of the main PictureBox control = False, the line drawings will be less faulty. Either that or set .HasBorder = False (both in the .frm file).
« Last Edit: June 20, 2020, 11:55:18 pm by FellippeHeitor »

#### Qwerkey

• Forum Resident
• Posts: 755
##### Re: Inside/Outside Triangle (After Problem Set by STxAxTIC)
« Reply #11 on: June 21, 2020, 03:57:14 am »
Perfect combo ...
@FellippeHeitor Like bplus, you are too kind.  This was only a little demo of my solution to STxAxTIC's problem.  I was going to enquire about the sometimes-ragged line drawing.  I shall know for next time.
InForm: The perfect resource for the inveterately low-skilled to produce quality work.