View Full Version : A question of math?

beardedpig

Dec 20, 2008, 09:48 AM

Picture a circle, it is 2" in diameter and has a circumference of 6.284.

I want to track a arc gesture in either direction clockwise/anti clockwise.

So I know D=2", R=1", C=6.284" and PI=3.142 but I do not know what theta is or arc.

apparently I should be able to work out arc, theta or C by knowing any of the two.

But as I do not know what arc or theta is.....I stumped. Also I am learning this stuff today!!

In my head I image a pie section of the circle with the one radius line going from 12 o'clock to circle centre and another radius line going from circle centre to 3 o'clock.

Now if the user traced a gesture along the circumference from 12 to 3 there would be a value for arc, true?

Now if the use traced the same path but inside the circle say half an inch in then the arc distance would be smaller but the angle (if that is the right term) would be the same as the first, true??

Problem: How would I begin it extrapolate the arc gesture in any direction within the circle? Are we taking quantum physics or is there a formular the uneducated such as I could learn as is the case for always working out the circumference of a circle?

thanks in hope

CommanderData

Dec 20, 2008, 10:58 AM

You can get the angle of the touchpoint in reference to the center of your circle by calculating the slope of the line and using arctan(slope). By determining the start and end angle of your touch you will have the arc they traced through.

Hope that gets you moving the right direction...

beardedpig

Dec 20, 2008, 12:41 PM

Mmm heavy math for the numpty who hated math and er never understood logarithms etc!!

So after some searching...

Tan(x) = y is the same as arctan(y) = x

I think I grasp a tad what you say but not completely.

If I am completely off beam please give me a gentle kick ;-)

If the user drags a straight path from A to B that would not have an angle would it?

But then as I type I start to ask aloud if I used the function angleBetweenLines this would return an angle even a very large one if there was any curve from start to end of drag....would it?

And if it did then it is this that I would pass to arctan to determine that the user performed a legal curve (arc) drag rather than a straight line?

beardedpig

Dec 20, 2008, 02:12 PM

I found this but is beyond my comprehension basically as it is not described in a more...basic way!

http://en.wikipedia.org/wiki/Arc_length

And this helps but again...struggling :(

http://en.wikipedia.org/wiki/Arc_(geometry)

CommanderData

Dec 20, 2008, 03:57 PM

Sometimes precision isn't as important as "good enough", don't bother with all the advanced calculus garbage...

Let's assume you're simulating the click-wheel on older iPods, and this click wheel is centered on your screen horizontally and vertically...

The center point of your wheel would be 160, 240. We'll call these cx and cy

The radius is 160 pixels.

Let's assume the "wheel" is a sensitive area from your outer radius and continuing 40 pixels inward toward the center.

We will assume the 0 degrees is straight up (12 oclock).

The first thing you want to determine is whether the users touch falls within the "wheel". Do this by calculating distance...

distance = sqrt(((touchx-cx)*(touchx-cx))+((touchy-cy)*(touchy-cy)))

if the distance is >= 120 pixels and <= 160 pixels from the centerpoint we know the finger is on the wheel, and continue on.

Now we want to determine our angle, we'll start by figuring the slope. But first... there are a few special cases of slope that will not work- when the users touch is in the wheel at 0 or 180 degrees the slope calculation would divide by zero!

if cx-touchx = 0, and if cy-touchy >0 then we're at 0 degrees

if cx-touchx = 0, and if cy-touchy <0 then we're at 180 degrees

if neither of these is true it's OK to try to calculate slope

slope = (cy-touchy)/(cx-touchx)

Then arctan(slope) returns something we can work with from 90 to -90 degrees. Now we need to correct this based on the quadrant the user's touch was in...

if touchx>cx and touchy<=cy then we're in the upper right quadrant and our true degree position is = 90 - arctan(slope)

if touchx>cx and touchy>cy then we're in the lower right quadrant and our true degree position is ALSO = 90 - arctan(slope). Simply because arctan(slope) will be negative and a double negative is the same as addition.

I'll leave it as an exercise to you to figure out the next two quadrants.

What I would do with all of this is take a starting reading in touchesBegan on your UIView, then take new readings at touchesMoved events. The larger the delta for previous to current angle, the faster the user is moving their finger in that direction... OR if you're simply interested in distance travelled you can take a reading in touchesBegan and another in touchesEnded and measure the difference between angles. Of course you'll need to account for travelling around more than one full revolution without removing the finger. That's why the delta readings may be easier to deal with.

Does this make any sense? :D

beardedpig

Dec 21, 2008, 05:28 AM

Sometimes precision isn't as important as "good enough", don't bother with all the advanced calculus garbage...

Oh thank God for that! I will glady take that approach!!

Let's assume you're simulating the click-wheel on older iPods, and this click wheel is centered on your screen horizontally and vertically...

I am not familiar with the wheel you describe as I do not own an iPod but understand your description :)

The center point of your wheel would be 160, 240. We'll call these cx and cy

The radius is 160 pixels.

Let's assume the "wheel" is a sensitive area from your outer radius and continuing 40 pixels inward toward the center.

We will assume the 0 degrees is straight up (12 oclock).

Yep, I'm with you and this is all straight forward :)

The first thing you want to determine is whether the users touch falls within the "wheel". Do this by calculating distance...

distance = sqrt(((touchx-cx)*(touchx-cx))+((touchy-cy)*(touchy-cy)))

if the distance is >= 120 pixels and <= 160 pixels from the centerpoint we know the finger is on the wheel, and continue on.

I understand the concept that we need to determine if the user is in this "wheel" .... nice approach ;-)

What I do not understand is sqrt(((touchx-cx)*(touchx-cx))+((touchy-cy)*(touchy-cy)))

1. I would never had figured to use sqrt or why it is required

2. why mst we multiply touchx - cx by the same thing touchx - cx

3. You see I have a limited knowledge about math like this but when explained in a certain way I will understand an learn and grow from it!

Now we want to determine our angle, we'll start by figuring the slope. But first... there are a few special cases of slope that will not work- when the users touch is in the wheel at 0 or 180 degrees the slope calculation would divide by zero!

if cx-touchx = 0, and if cy-touchy >0 then we're at 0 degrees

if cx-touchx = 0, and if cy-touchy <0 then we're at 180 degrees

I understand this but again will need to draw this on paper to visualise and better understand :-)

if neither of these is true it's OK to try to calculate slope

slope = (cy-touchy)/(cx-touchx)

Then arctan(slope) returns something we can work with from 90 to -90 degrees. Now we need to correct this based on the quadrant the user's touch was in...

if touchx>cx and touchy<=cy then we're in the upper right quadrant and our true degree position is = 90 - arctan(slope)

if touchx>cx and touchy>cy then we're in the lower right quadrant and our true degree position is ALSO = 90 - arctan(slope). Simply because arctan(slope) will be negative and a double negative is the same as addition.

I'll leave it as an exercise to you to figure out the next two quadrants.

Yes I agree as to be shown the complete answer does no encourage the grey matter to expand beyond its limited container....lol ;-))

What I would do with all of this is take a starting reading in touchesBegan on your UIView, then take new readings at touchesMoved events. The larger the delta for previous to current angle, the faster the user is moving their finger in that direction... OR if you're simply interested in distance travelled you can take a reading in touchesBegan and another in touchesEnded and measure the difference between angles. Of course you'll need to account for travelling around more than one full revolution without removing the finger. That's why the delta readings may be easier to deal with.

I need to make an action take place within a certain degree of movement so will be using both touches began and touches moved. Had not thought of the more than one revolution but will deal with that once I have the main part working.

Does this make any sense? :D

Yes it makes sense although there are parts I do not understand I do grasp the concept which I am grateful you took the time to help me and from which I hope the brain will allow me to grow with :)

I will spend today plotting this into my app to see how I get on :)

beardedpig

Dec 21, 2008, 07:08 AM

Cool!! here is my code so far that determines the user clicking in the "wheel"

the .h file

#define kCircleCentreX 160

#define kCircleCentreY 240

#define kInnerTouchBound 80

#define kOuterTouchBound 158

You see I have made the touch width slightly wider purely for personal taste of course and I have also set the outer bound to just inside the circle.

the .m file

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event

{

UITouch *touch = [touches anyObject];

CGPoint currentPosition = [touch locationInView: self.view];

NSUInteger numTaps = [[touches anyObject] tapCount];

int distanceFromCentre;

distanceFromCentre = sqrt(((kCircleCentreX - currentPosition.x) * (kCircleCentreX - currentPosition.x)) + ((kCircleCentreY - currentPosition.y) * (kCircleCentreY - currentPosition.y)));

// just test using taps for now, slide will come in a mo...he hopes!

if ( numTaps == 1 && numTouches == 1 )

{

if ( distanceFromCentre >= kInnerTouchBound && distanceFromCentre <= kOuterTouchBound )

{

NSLog(@"wheel area tapped");

}

}

}

For me this has beent he best learning exercise I have had so far with the SDK....you are a star thank you :)

sammich

Dec 21, 2008, 07:30 AM

What are you simulating exactly? Something like a rotary dial on an old telephone? Well, you've taken your first step, albeit small, it's good to see that you are grasping some of the concepts. But you must be warned...since you needed this level of guidance on this concept, I'm afraid you better take up a liking for maths, because maths is no small part of finding the optimal solution to a problem.

The easiest way to grab a pen and paper and figure it out that way, especially for the solution below, simple trigonometry was all that was needed.

beardedpig

Dec 21, 2008, 08:09 AM

What are you simulating exactly? Something like a rotary dial on an old telephone? Well, you've taken your first step, albeit small, it's good to see that you are grasping some of the concepts. But you must be warned...since you needed this level of guidance on this concept, I'm afraid you better take up a liking for maths, because maths is no small part of finding the optimal solution to a problem.

The easiest way to grab a pen and paper and figure it out that way, especially for the solution below, simple trigonometry was all that was needed.

I could not agree more and fully understand the importance of math, but alas if you dont use it you loose it, correct?

I have learnt some stuff in the past but not using it all the time I forget it.

Like for example my pondering the sqrt issue given.

It is the 3, 4, 5 equation which is coming back to me and now I remember using this when creating a set square years ago to lay slabs!

However as the saying goes...teach a man to fish etc etc.

It is a pleasure to find people here willing to teach thus help other rather than flame them for not knowing something they have not learned yet :)

I am now plotting on paper etc and looking for basc trig website to teach this lump of tissue in my head I have been told is a brain....lol :)

aha!

http://www.waybuilder.net/free-ed/Math/Trigonometry/trig02_SPK.asp

CommanderData

Dec 21, 2008, 08:47 AM

BeardedPig, glad I could nudge you in the correct direction and that you appreciate the effort I put into my response. A lot of times people are ungrateful and just looking for you to write their program for them... it is refreshing to see someone trying hard to learn some math and understand new concepts :)

Keep it up and let us know how your work progresses!

beardedpig

Dec 21, 2008, 10:01 AM

BeardedPig, glad I could nudge you in the correct direction and that you appreciate the effort I put into my response. A lot of times people are ungrateful and just looking for you to write their program for them... it is refreshing to see someone trying hard to learn some math and understand new concepts :)

Keep it up and let us know how your work progresses!

you are welcome :)

I am currently learning how to calculate square roots without a calculator.

This was so I could come back here and write down how I have understood your example.

So I now know from the 3,4 5 rule what you were doing in:

distance = sqrt(((touchx-cx)*(touchx-cx))+((touchy-cy)*(touchy-cy)))

example:

touchx = 310, touchy = 200 (so we know this is inside the wheel)

centrex = 160, centrey = 240

so...

(touchx-centrex)*(touchx-centrex) is the same as (310 - 160)*(310 - 160) is the same as 3 * 3 in the 3, 4 5 rule.

so...

(touchy-centrey)*(touchy-centrey) is the same as (200 - 240)*(200 - 240) is the same as 4 * 4 in the 3, 4 5 rule.

so...

(310 - 160)*(310 - 160) = (150 * 150 ) = 22500

(200 - 240)*(200 - 240) = (-40 * -40) = 1600

so...

distance = sqrt(((touchx-centrex)*(touchx-centrex))+((touchy-centrey)*(touchy-centrey)))

=

distance = sqrt(22500 + 1600) = sqrt(24100)

so...

distance = 155.24

so...

155 is greater than 80 (the inner touch boundary) and is less than 158 (the outer touch boundary)

ergo the user is in the wheel!

I hope I have expanded this out correctly as it makes sense to me! lol

Ah tis good to exercise the brain :)

touchx =

beardedpig

Dec 22, 2008, 08:06 AM

So the exercise is coming along nicely apart from the confusion I have with the values I am getting back when trying to obtain the real degrees

from 12 o'clock to 3 o'clock I get small decimal variations in values from 91 to 90

from 3 o'clock to 6 o'clock I get small decimal variations in values from 89 to 88

from 6 o'clock to 9 o'clock I get small decimal variations in values from 91 to 90

from 9 o'clock to 12 o'clock I get small decimal variations in values from 89 to 88

all my atan(slope) values range from -0, -1, 0 and 1?

beardedpig

Dec 22, 2008, 10:51 AM

still a rummagin...

finds that atan is not the same as arctan in as much it takes radians rather than a slope?

Still trying to find how to use arctan in Xcode.....where is that dastardly function??? grrrr

YMark

Dec 22, 2008, 11:49 AM

Interesting read. Reminds me of my days in college. I'd help out but I'm afraid I've lost that knowledge years ago.

beardedpig

Dec 22, 2008, 12:03 PM

Interesting read. Reminds me of my days in college. I'd help out but I'm afraid I've lost that knowledge years ago.

lol yeah....if only I paid attention! hee hee

But I have now found the following

slope = -1.066444

atan(slope) = -0.817541

arctan(slope) = -46.84164

as per the useful website : http://www.analyzemath.com/Calculators_2/arctan_calculator.html

trying to find maybe Math.toDegrees(atan(slope)) but this errors...

:)

beardedpig

Dec 22, 2008, 02:40 PM

the continued journey of BeardedPig to comprehend something he should have learned many many .... many years ago!

But a documented journey nonetheless that may one day save another soul such as I the same journey by providing the solution here....LOL :)

So perhaps he thinks oh if I know the radian vale of the slope I could find out how many radians to a degree or vice versa.

But no wait a moment, he finds this page which makes him ponder maybe it is better to work in radians rather than degrees?

http://id.mind.net/~zona/mmts/trigonometryRealms/radianDemo1/RadianDemo1.html

Tune in next time folks for the next exciting installment in this epic.....thread lol :)

dejo

Dec 22, 2008, 03:02 PM

But no wait a moment, he finds this page which makes him ponder maybe it is better to work in radians rather than degrees?

Normally it is better to work in radians rather than degrees, since many of the trigonometric functions take radians as the input.

beardedpig

Dec 22, 2008, 03:14 PM

Normally it is better to work in radians rather than degrees, since many of the trigonometric functions take radians as the input.

Yes this is what I am finding from my readings!

However to you my audience yes the bleeding obvious has finally kicked me up the a'hole!

If I simply divide the result from atan(slope) I get the degress!

Holy crap that is my problem with this sort of guff! I go all around the bleediin houses just to get to a simple equation!!!!!

Hey ho all in the learning, now back to determining the quadrants :)

CommanderData

Dec 22, 2008, 03:22 PM

I'm sorry to see you're hitting a wall, but it is good that you're sharing with everyone for future reference!

As you've seen, the atan function is returning radians. You can always convert to degrees for your own sanity if it's too alien to think in radians. Just multiply your sample value by 180/pi

atan(slope) = -0.817541

-0.817541 * (180/pi) = -46.84

a shortcut would be just to pre-calculate the 180/pi

myAngle = atan(slope) * 57.295779513

That should be plenty accurate enough for you! :D

beardedpig

Dec 22, 2008, 03:38 PM

I'm sorry to see you're hitting a wall, but it is good that you're sharing with everyone for future reference!

As you've seen, the atan function is returning radians. You can always convert to degrees for your own sanity if it's too alien to think in radians. Just multiply your sample value by 180/pi

atan(slope) = -0.817541

-0.817541 * (180/pi) = -46.84

a shortcut would be just to pre-calculate the 180/pi

myAngle = atan(slope) * 57.295779513

That should be plenty accurate enough for you! :D

more elegant than my method :)

For me this has been a great exercise and has opened up many doors that were otherwise shut to me, even though I have had to shoulder the buggers open!

And the good thing is I fully understand the problem and subsequent solution. It will be cool if my insane ramblings help others in the future who come across a similar problem.

Also as a firs dive into this forum my welcome has been fantastic. Thanks to all who commented especially CommanderData for the pointer to the solution and to all others who chose not to flame me which happens on most forums!

:):)

Pikemann Urge

Dec 31, 2008, 09:08 PM

http://www.waybuilder.net/free-ed/Math/Trigonometry/trig02_SPK.asp

Hey thanks for this.

The iPhone has got me interested in programming again. Whether I'll do anything with it is another matter. Still, re-learning basic trigonometry can't hurt!

beardedpig

Jan 4, 2009, 09:32 AM

well I have come on leaps and bounds! I invested in three downloadable videos from here:

http://mathmadesimple.com/

and am happily working out the problems I had and am solving each one in turn :)