Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Want to take a look at some math?

Author
Message
basicFanatic
6
Years of Service
User Offline
Joined: 7th Jun 2017
Location:
Posted: 28th Apr 2020 14:58
So, I have these values:

aVal# = 310
bVal# = 320

x = 100
xMax = 200

x and xMax work like a percentage system, with xMax representing 100%.
but in the case above I set xMax to 200 and set x to 100, so here, x works like 50 %

I want to calculate a value that is between aVal# and bVal#, and use x to slide between them.
In the example above x is practically 50 %, so the result would be smack in the middle, that is 315
If x was 0 we would get 310
if x was 200 we would get 320

What should I do to get those results?

If you're curious, I'm trying to place a string of brush dabs, the number depending on how far the cursor have moved.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 28th Apr 2020 15:13 Edited at: 28th Apr 2020 15:13
Here's a snippet from my personal maths library:

I think that's what you're looking for.
Qugurun
Valued Member
9
Years of Service
User Offline
Joined: 8th Dec 2014
Playing: AppGameKit
Posted: 28th Apr 2020 16:34

hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 28th Apr 2020 16:55 Edited at: 28th Apr 2020 16:56
Scraggle's function should work if your control range (x) needs to start at any value other than 0. If not, you can just use this formula:

result# = aVal# + (bVal# - aVal#) * (x# / xMax#)

x and xMax need to be declared as floats for it to work.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Apr 2020 18:11
What you want is called linear interpolation.

value = aVal# + (bVal#-aVal#) * t

Where 't' is your percentage value, a decimal between 0 and 1. If you need to calculate that percentage value: (these should all be floats)

max_range = bVal#-aVal#

percent = current_value / max_range
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
basicFanatic
6
Years of Service
User Offline
Joined: 7th Jun 2017
Location:
Posted: 28th Apr 2020 20:05
Thanks all!
Getting the brush strokes to scale correctly over time was That Big Task, the one I really didn't think I could pull off. Partly because I needed to pull apart a lot of the code to fit it in. But this morning I woke early and wrote some pseudocode which sounded about right. Then I tried it and ... nope, didn't work at all, the brush daps were all over the place. I found out what line gave the trouble, asked around here, copypasted Scraggles' code and ...yet again it didn't work. Then I found out I had switched around some parameters, and stuff just worked! Honestly, it felt magical.

Login to post a reply

Server time is: 2024-04-24 20:09:28
Your offset time is: 2024-04-24 20:09:28