Sign in to follow this  
karmakrazy13

Math Library C# Help

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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;
}




}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this