Write a VBA program that reads the coordinates of three data points from \"Sheet
ID: 3664769 • Letter: W
Question
Write a VBA program that reads the coordinates of three data points from "Sheet2": cell "A1" and "B1"(the x and y coordinates of the first data point); cell "A2", "B2"(the x and y coordinates of the second data point); cell "A3", "B3"(the x and y coordinates of the third data point). The program then determines if a triangle could be formed using these three data points as the vertexes and displays a message accordingly. For example:
" Yes they could form a triangle . And the three sides are 3.2, 1.9, and 4." .
or,
" No triangle could be formed using these three data points."
Explanation / Answer
Here is the code as per your requirements.
Dim a As Single
Dim b As Single
Dim c As Single
Dim x1 As Single
Dim x2 As Single
Dim x3 As Single
Dim y1 As Single
Dim y2 As Single
Dim y3 As Single
x1 = Range("A1")
y1 = Range("B1")
x2 = Range("A2")
y2 = Range("B2")
x3 = Range("A3")
y3 = Range("B3")
a = sqrt(Sqr(x2 - x1) - Sqr(y2 - y1))
b = sqrt(Sqr(x3 - x2) - Sqr(y3 - y2))
c = sqrt(Sqr(x1 - x3) - Sqr(y1 - y3))
If (a + b > c And b + c > a And c + a > b) Then
MsgBox " Yes they could form a triangle . And the three sides are" & a & b & c
Else
MsgBox " No triangle could be formed using these three data points."
End If
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.