Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I apply the Pythagorean Theorem to ROBLOX?

Asked by 10 years ago

I'm sure most of you know what that is, if not I could give an explanation.

a^2 + b^2 = c^2

Here is a definition of it.

"In mathematics, the Pythagorean theorem—or Pythagoras' theorem—is a relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse is equal to the sum of the squares of the other two sides" (I got this from the Wikipedia.)

c is the side across from the right angle being the hypotenuse.

Say I lean a part at an angle against a wall in roblox, I want to find the hypotenuse.

My first question is, how would I even begin measuring? would I go by studs??

So if we are doing studs then here is another example.

I want to find the distance across a square shaped part.

Each side of the square is 1 stud long.

Step 1. a^2 + b^2 = c^2

Step 2. 1^2 + 1^2 = c^2

Step 3. 1 + 1 = c2

Step 4. 2 = c2

Step 5. c2 = 2

Step 6. c = (SquareRootSymbol)2 = 1.4142... (The answer)

What I am trying to do is make a roblox defined equation for automatically finding the hypotenuse for a part on roblox. Either it be a triangle or just a square part. What should I do to make it fully functional? and transfer a^2 + b^2 = c^2 to roblox form? Sort of like a Pythagorean theorem calculator.

Imagine a box get cut in half, then it will measure the length and the height size. Then square them. THEN it will add the two together giving you the hypotenuse but... the hypotenuse must be square rooted. Then the length across the square will be given.

1
Could you please provide a screenshot of roblox, with some drawings on top of it to show what you want? I'm having trouble understanding your situation. User#11893 186 — 10y
0
I will when I get home. When you select by doing game.Workspace.Part or whatever.. It will measure the sizes of each side being A and B. It will then square the sizes then add them together and it will then find C^2.. Then it will find the square root of C, returning the length of the hypotenuse. IntellectualBeing 430 — 10y
1
To get the length of the sides of a rectangular-shaped part, you can just use part.Size.X and part.Size.Z. These properties are measured from the center, but they will be the same on the edges as well. If those are your `a` and your `b`, then you can use the function I provided below to find the length hypotenuse of our triangle, which will be the length of the line if you were to draw a diagonal User#11893 186 — 10y
0
This helped, Thanks! IntellectualBeing 430 — 10y
1
"... draw a diagonal line from the top-left corner to the bottom-right, assuming you were looking straight down. " No problem. User#11893 186 — 10y

3 answers

Log in to vote
1
Answered by 10 years ago

To create a simple function to do this, you can use this:

function pythag(a, b)
    return math.sqrt(a^2 + b^2)
end


pythag(3, 4)
--> 5

However, I can't tell if there is more to your question here. If you were to edit your question and include a diagram or something similar, I might be able to help you more effectively.

Ad
Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

I used it to find a part's actual velocity through the X & Z plane. In this example, I did this to detect the player's HumanoidRootPart's velocity (walkspeed (if you're walking)).

s = script.Parent
repeat wait() until s ~= nil
while true do
    wait()
    local reference = tostring(math.floor(math.abs(s:findFirstChild("HumanoidRootPart").Velocity.x)))..", "..tostring(math.floor(math.abs(s:findFirstChild("HumanoidRootPart").Velocity.y)))..", "..tostring(math.floor(math.abs(s:findFirstChild("HumanoidRootPart").Velocity.z)))
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.TextLabel.Text = reference

    local ok = math.floor(math.sqrt(math.pow((tostring(math.abs(s:findFirstChild("HumanoidRootPart").Velocity.x)), 2)+(math.pow(tostring(math.abs(s:findFirstChild("HumanoidRootPart").Velocity.z)), 2)))
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.TextLabel2.Text = ok
end

Lol, so messy... If you want to see the code easier, just copy & paste this to an actual script.

Log in to vote
-4
Answered by 10 years ago
function pytha(a,b,c)
end
--Something like this, where you can insert your things as a,b, and c. I'm not making the equation for you because I am not sure what you want out of this, but basically you can call the function as below, and insert whatever numbers you want as a,b, and c.
pytha(1,2,3)
0
"I want to find the distance across a square shaped part." IntellectualBeing 430 — 10y

Answer this question