0 Replies - 953 Views - Last Post: 11 November 2008 - 02:59 AM Rate Topic: -----

#1 slacktivist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-November 08

Need help with a couple of formula (Please Read)

Posted 11 November 2008 - 02:59 AM

Hey guys, I have a school problem that I need help with. I am not going to get on here and ask you for the answer, instead I am going to ask you to help walk me down the right path. I have researched the problem and potential answers but can't seem to figure out how to apply it, this is where I need your help. It is a three part question, if ANYONE can help with ANY of it, I would be very grateful.

*** Questions I need help with.
1. A function that calculates the distance between two(2D) points

Answer - I was able to figure this one out on my own, it is the three below this one I need help with

2. A function that calculates the angle between two(2D) vectors

The formula that I think would work is
float angle = (float)Math.Acos(Vector3.Dot(VectorA, VectorB));
, the problem I am running into is figuring out first if this is the formula and second what needs to be substituted.

3. A function that tells if a point(2D) is within a circle

Again, I believe that the forumula is
double x = Math.Cos(theta) * width * length;
double y = Math.Sin(theta) * height * length;



4. A function that tells if a point(2D) is visible, given a view location and direction

??? I am so very lost on this one, and after searching for any help for about 4 hours... I am getting a bit frustrated with this, (to say the least!!)

My code as of late.

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");

		// test the distance between two points function
		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;
	}
	
}




I am a graphic design major and would be more then willing to help you any and all questions and would be willing to design something for you in return for you time and help.

Thank you for your help!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1