Math Library C# Help

Started by
10 comments, last by Crazyfool 15 years, 7 months ago
I am using Microsoft visual studio 2008 to do this. I need to figure out: A function that calculates the distance between two(2D) points A function that calculates the angle between two(2D) vectors A function that tells if a point(2D) is within a circle A function that tells if a point(2D) is visible, given a view location and direction if anyone could help me that would be amazing!
Advertisement
This is all fairly basic math. Are you doing this as a homework assignment? Show us what you have tried first, and then we'll help you work your way through it.
Mike Popoloski | Journal | SlimDX
using System;

public class Simple
{
static void Main(string[] args)
{
// test our Normalize function
float nX = 0;
float nY = 0;
Normalize(1, 1, ref nX, ref nY);
Console.WriteLine("Normalizing the vector ( 1, 1 )\nresult - ( " + nX + ", " + nY + " )\n");

// test the GetDistFromRay function
float dist = GetDistFromRay(0, 0, 1, 0, 5, 3);
Console.WriteLine("Finding the smallest distance between a line rayPoint(0, 0), rayDirection(1, 0) and a point (5, 3)\nresult - " + dist + "\n");

// test the RayIntersectsCircle function
Console.WriteLine("Does the ray from above intersect a circle with a radius of 4 at the same spot as the point?");
if (RayIntersectsCircle(0, 0, 1, 0, 5, 3, 4))
Console.WriteLine("result - true\n");
else
Console.WriteLine("result - false\n");

dist = DistanceFormula(1, 1, 1, 8);
Console.WriteLine("Distance between the two points ( 1, 1 ) is: " + dist);
}


////////////////////////////////////////////

static bool RayIntersectsCircle(float rayX, float rayY, float rayDirX, float rayDirY, float circleX, float circleY, float radius)
{
// find the distance of the circle from the ray
float dist = GetDistFromRay(rayX, rayY, rayDirX, rayDirY, circleX, circleY);

// and compare that distance to the radius
if (dist <= radius)
return (true);
else
return (false);
}

////////////////////////////////////////////

static float GetDistFromRay(float rayX, float rayY, float rayDirX, float rayDirY, float ptX, float ptY)
{
// get the vector from the raypoint to the position of the point
float tX = ptX - rayX;
float tY = ptY - rayY;
// get the dot product of the ray direction and our temporary vector
float dot = tX * rayDirX + tY * rayDirY;
// get the point on the line
float lineX = rayX + (rayDirX * dot);
float lineY = rayY + (rayDirY * dot);

// set up variables for the distance test
float dx = lineX - ptX;
float dy = lineY - ptY;

// return the distance
return ((float)Math.Sqrt(dx * dx + dy * dy));
}

////////////////////////////////////////////

static void Normalize(float x, float y, ref float outX, ref float outY)
{
// first find the length of the vector, which is essentially the distance between( x, y ) and ( 0, 0 )
float length = (float)Math.Sqrt(x * x + y * y);

// divide x and y by the length, so that the vector will have a length of 1, and then store the result in the out variables
outX = x / length;
outY = y / length;
}

////////////////////////////////////

static float DistanceFormula(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
dx = dx * dx;
dy = dy * dy;

float dist = (float)Math.Sqrt(dx + dy);

return dist;
}




}
Looking around for information on the basics of vector mathematics might help you as well, although if this is for homework its all likely in your textbook as well.
Quote:Original post by jpetrie
Looking around for information on the basics of vector mathematics might help you as well, although if this is for homework its all likely in your textbook as well.


I wish it was in my textbook, but it is not in there anywhere

Sure, the actual source code most likely isn't there, but show me any linear algebra book that doesn't go over these concepts.

Also, don't just vomit your code up here and expect us to magically know what's wrong. Give us small bits showing a piece of code that isn't working as expected. Show us what you're getting, and what you expect to be getting. Learn to use your debugger.

Finally, use the [ source ][ /source ] tags to make your code readable on the forums.
Mike Popoloski | Journal | SlimDX
I do not know how to start the code for what I need...I am uber newb
Is this for homework or not?
SGD PROGRAMMING

It is our first project
Class is SGD Programming
Okay, well, as I said, the information is all over the internet -- all you need to do is search for "vector math" and you get results like
this, for example, and this. "2D geometry" might be helpful as well, also "circle containment test," for example. Results like those should help you figure out what you need to do from a high-level, algorithmic perspective.

Remember also that you can represent a point or a vector in 2D space as a pair of values, ideally floats. You can encapsulate them in a struct in C# for easy access:
struct Point2D {  public float X;  public float Y;}

and implement the operations you need as methods operating on those Point2D or Vector2D or what have you objects.

This topic is closed to new replies.

Advertisement