How I used high school math to solve a programming problem!
Let me start by confessing that the problem I’m going to show you is not strictly rocket science. It is rather simple. Few may argue that we don’t need to use math to solve this. However, my intention behind writing this blog is just to counter the claims that large proportion of people make that the math taught to us in high school is practically useless and there will not be any case of real world application. Not to display high understanding of mathematics.
To those who say high school math is just useless with respect to practical application, I’d say `It depends!`. It depends on the field you are in and the opportunities you get to apply the math. I got lucky enough to encounter a small problem where I had the opportunity to use it. Here it goes..
We are developing a component in our application which produces PDFs with a water mark. The watermark can be multiline and can be placed at an angle. This is an example
The three lines of watermark text and the angle is configurable. Meaning, an end user of the application can select the three lines of watermark text individually and also specify the angle at which the watermark should be rotated. Along with the text and angle, user can also select the position (x,y coordinate) of the first line of text.
So what we know is the position of the first line of text, the displacement of y coordinate for the second and third lines of text and the angle at which the text is to be rotated.
Now the question is, how much should the x coordinate be displaced by for the second and third lines of the watermark depending on the angle of rotation. This is important to know because without displacing the x coordinate or with wrong amount of displacement, the watermark would appear like this.
Which is just wrong!
So, how to figure out the necessary displacement of x coordinate based on the angle of rotation.
So. Let’s imagine that the gap formed at the beginning of the first line as a right angled triangle. Naturally, what we need to find is the base of the right angled triangle, and that should be how much the x coordinate of the next line should be moved by. Illustrated in the figure below
We need to figure out the correct value of x based on the angle (θ) and we know y. How do we do that?
If we recollect the trigonometric functions..
So in our case, as we know the angle and the adjacent side, the opposite side, which is the length we are looking for, is just
tan(angle) multiplied by the adjacent side.
And that is all. For every new line of text we add, we know that the value of x needs to be incremented by the value derived above.
There are applications of mathematics in various fields of science and technology. The algorithms involved in the field of Machine Learning and Artificial Intelligence are an example of such an application. You may not be such a field. Or you may not have chosen to be in such a field. It is easy to disregard the importance of high school education by saying you never got to use it. You just aren’t put in the right circumstances to use it. Or you are not able to use it even under the right circumstances.
Only you would know!!
Did you find this article valuable?
Support Pavan Andhukuri by becoming a sponsor. Any amount is appreciated!