Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dahaka666 · Feb 14 at 04:39 AM · mathdatarangeconversionformula

Convert linear range to another range, maintaining ratio, but without knowing the original minimum and maximum

So, I am working with a dataset that contains a variable which can have a value of various ranges that I am unable to predetermine. The range is a value that can range from -x to x, with the median at 0, and x could be a range such as -15 to 15, or it could be -1 to 1. The data I receive is more based on the deviation from 0 than from the range min to the range max, and I believe this is vital to the formula.

I am trying to convert the range of -x to x, to be either in the range of 0 to 100, or -1 to 1, all while maintaining the original ratios. I have taken a look at this stack overflow question: "Convert a number range to another range, maintaining ratio" but the formulas there require a known and predetermined range min and max to start with. Math is certainly not my strong suit and I am at a total loss of how to convert this data, so any help here would be greatly appreciated.

If it does help any, the data I receive is in a list/array, something like:

x[]:{ 1.47f, 1.21f, 0.49f, -0.05f, -0.69f, -1.8f, -2.35f, -1.2f, -0.42f, 4.56f }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dahaka666 · Feb 14 at 05:27 AM 0
Share

My ultimate goal with this is to graph the data onto a UI line graph of size x:200, y:100, with the 0 value being centered on the Y axis, and the graph being plotted over time across the x axis. So, the minimum value should be the lowest value on the graph, being 0 on the y axis, and the maximum value should be 100 on the y axis, with the middle values being plotted between these values while maintaining the ratio between them.

avatar image elenzil Dahaka666 · Feb 14 at 07:23 PM 0
Share

this comment greatly clarifies your question. I'd say you Do know the input range: it's the min/max of the data values. With that in hand, @BastianUrbach 's answer has all you need.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BastianUrbach · Feb 14 at 08:01 AM

It sounds like you're just looking for Mathf.Lerp and Mathf.InverseLerp (aka linear interpolation and inverse linear interpolation). Lerp maps 0-1 to any other range while InverseLerp maps any range to 0-1. By combining the two, you can easily map any range to any other range:

 var t = Mathf.InverseLerp(oldMin, oldMax, value);
 value = Mathf.Lerp(newMin, newMax, t);


The math behind it is not too complicated:

 Lerp(a, b, t) = (1 - t) * a + t * b
 InverseLerp(a, b, v) = (v - a) / (b - a)


IMO the best way to understand why this works is to check the following requirements by inserting the values in the formulas above:

When t = 0, Lerp must be a.
When t = 1, Lerp must be b.
When v = a, InverseLerp must be 0.
When v = b, InverseLerp must be 1.

Edit:
After reading your question again, what exactly do you mean by "without knowing the original minimum and maximum"? You know the values, right? So what's stopping you from determining the minimum and maximum?

 var min = Mathf.Min(array);
 var max = Mathf.Max(array);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distance in a Trajectory of a projectile 0 Answers

Physic Material friction mathematical formula 0 Answers

health bar position formula 3 Answers

Math question about random range and fractions 2 Answers

Lat Long to Unity world coordinates 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges