<commit_before>﻿using Microsoft.Xna.Framework;


namespace Nez.PhysicsShapes
{
	public static partial class ShapeCollisions
	{
		public static bool lineToPoly( Vector2 start, Vector2 end, Polygon polygon, out RaycastHit hit )
		{
			hit = new RaycastHit();
			var normal = Vector2.Zero;
			var intersectionPoint = Vector2.Zero;
			var fraction = float.MaxValue;
			var hasIntersection = false;

			for( int j = polygon.points.Length - 1, i = 0; i < polygon.points.Length; j = i, i++ )
			{
				var edge1 = polygon.position + polygon.points[j];
				var edge2 = polygon.position + polygon.points[i];
				Vector2 intersection;
				if( lineToLine( edge1, edge2, start, end, out intersection ) )
				{
					hasIntersection = true;

					// TODO: is this the correct and most efficient way to get the fraction?
					// check x fraction first. if it is NaN use y instead
					var distanceFraction = ( intersection.X - start.X ) / ( end.X - start.X );
					if( float.IsNaN( distanceFraction ) )
						distanceFraction = ( intersection.Y - start.Y ) / ( end.Y - start.Y );

					if( distanceFraction < fraction )
					{
						var edge = edge2 - edge1;
						normal = new Vector2( edge.Y, -edge.X );
						fraction = distanceFraction;
						intersectionPoint = intersection;
					}
				}
			}

			if( hasIntersection )
			{
				normal.Normalize();
				float distance;
				Vector2.Distance( ref start, ref intersectionPoint, out distance );
				hit.setValues( fraction, distance, intersectionPoint, normal );
				return true;
			}

			return false;
		}


		public static bool lineToCircle( Vector2 start, Vector2 end, Circle s, out RaycastHit hit )
		{
			hit = new RaycastHit();
<commit_msg>Handle vertical lines in ShapeCollision.lineToPoly.<commit_after>﻿using Microsoft.Xna.Framework;


namespace Nez.PhysicsShapes
{
	public static partial class ShapeCollisions
	{
		public static bool lineToPoly( Vector2 start, Vector2 end, Polygon polygon, out RaycastHit hit )
		{
			hit = new RaycastHit();
			var normal = Vector2.Zero;
			var intersectionPoint = Vector2.Zero;
			var fraction = float.MaxValue;
			var hasIntersection = false;

			for( int j = polygon.points.Length - 1, i = 0; i < polygon.points.Length; j = i, i++ )
			{
				var edge1 = polygon.position + polygon.points[j];
				var edge2 = polygon.position + polygon.points[i];
				Vector2 intersection;
				if( lineToLine( edge1, edge2, start, end, out intersection ) )
				{
					hasIntersection = true;

					// TODO: is this the correct and most efficient way to get the fraction?
					// check x fraction first. if it is NaN use y instead
					var distanceFraction = ( intersection.X - start.X ) / ( end.X - start.X );
					if( float.IsNaN( distanceFraction ) || float.IsInfinity( distanceFraction ) )
						distanceFraction = ( intersection.Y - start.Y ) / ( end.Y - start.Y );

					if( distanceFraction < fraction )
					{
						var edge = edge2 - edge1;
						normal = new Vector2( edge.Y, -edge.X );
						fraction = distanceFraction;
						intersectionPoint = intersection;
					}
				}
			}

			if( hasIntersection )
			{
				normal.Normalize();
				float distance;
				Vector2.Distance( ref start, ref intersectionPoint, out distance );
				hit.setValues( fraction, distance, intersectionPoint, normal );
				return true;
			}

			return false;
		}


		public static bool lineToCircle( Vector2 start, Vector2 end, Circle s, out RaycastHit hit )
		{
			hit = new RaycastHit();

{'fraction_non_alphanumeric': 0.10087209302325581, 'fraction_numerical': 0.009883720930232558, 'mean_word_length': 2.848993288590604, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4333038', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1031, 'n_words': 326}