WateR
WateR

Reputation: 1

Getting the opposite angle of a circle

I'm trying to code in something that gets a random angle from 0 to 359 and then gets the opposite angle from that random angle.

double? AngleNoCur = randomAngle ? Random.Next(0, 359) : angle * Math.PI / 180; //'angle' is used for something else, ignore it
double? fixedAngleOpposite = AngleNoCur <= 179 ? AngleNoCur + 180 * Math.PI / 180 : AngleNoCur - 180 * Math.PI / 180;

After that I have the part where you add the x, y with (cos) and (sin). I'm sure that's not the problem because I can just put a random angle and it will go to that random angle.

Also negative angles do not work.

Any help would be appreciated!

Upvotes: 0

Views: 1176

Answers (1)

MetaColon
MetaColon

Reputation: 2871

I think you're being confused with radiants. The equation AngleNoCur + 180 * Math.PI / 180 isn't making much sense for me - you're adding 180 divided by 180 and multiplied by Pi... I think you want you're result in degrees, so just leave that part - 180 is already in degrees. Then it would look like that:

double? fixedAngleOpposite = AngleNoCur <= 179 ? AngleNoCur + 180 : AngleNoCur - 180;

Furthermore I wouldn't substract 180 degrees under special circumstances - I'd use Modulo:

double? fixedAngleOpposite = (AngleNoCur + 180) % 360;

However, if you want to get radiants in the end you can use this:

fixedAngleOpposite *= Math.PI / 180;

Actually I think you constantly wanted to work with radiants, because you converted the angle - whatever it is, we should ignore it - to radiants. Then your part with the fixedAngleOpposite should work, but you should alter the AngleNoCur:

double? AngleNoCur = randomAngle ? Random.Next(0, 359) * Math.PI / 180 : angle * Math.PI / 180;

However, I'd still suggest using Modulo for fixedAngleOpposite and you can shorten your equation:

double? fixedAngleOpposite = (AngleNoCur + Math.PI) % (2 * Math.PI);

For more information about degrees and radiants I'd suggest to read through this: http://www.vcskicks.com/csharp_net_angles.php. For more information about Modulo, take a look at this: https://en.wikipedia.org/wiki/Modulo_operation.

Upvotes: 1

Related Questions