How to Repair Argument is Out of Range

Hello, this is the first time I ask in this forum. I hope you all can help me. Thank you.

I really confuse with this error because this is the first I make little bit serious project in unity, so I confuse with the error. This error is appear after the line in screen is drawn.

This is the picture of error :

And this is the code. I use someone code to the draw function and then add code Monotone Chain Convex Hull algorithm for check the convex hull of line which drawn. Anyone can help me please ?

And this is the full code :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class DrawLine : MonoBehaviour 
{
	private LineRenderer line;
	private bool isMousePressed;
	private List<Vector3> pointsList;
	private List<Vector3> H;
	private List<Vector3> simpanConvex;
	private List<Vector3> convexPlus;
	private Vector3 mousePos;

	// Structure for line points
	struct myLine
	{
		public Vector3 StartPoint;
		public Vector3 EndPoint;
	};
	//	-----------------------------------	
	void Awake()
	{
		// Create line renderer component and set its property
		line = gameObject.AddComponent<LineRenderer>();
		line.material =  new Material(Shader.Find("Particles/Multiply"));
		line.SetVertexCount(0);
		line.SetWidth(0.1f,0.1f);
		line.SetColors(Color.green, Color.yellow);
		line.useWorldSpace = true;	
		isMousePressed = false;
		pointsList = new List<Vector3>();
//		renderer.material.SetTextureOffset(
	}
	//	-----------------------------------	
	void Update () 
	{
		// If mouse button down, remove old line and set its color to green
		if(Input.GetMouseButtonDown(0))
		{
			isMousePressed = true;
			line.SetVertexCount(0);
			pointsList.RemoveRange(0,pointsList.Count);
			line.SetColors(Color.green, Color.yellow);
		}
		else if(Input.GetMouseButtonUp(0))
		{
			isMousePressed = false;
		}
		// Drawing line when mouse is moving(presses)
		if(isMousePressed)
		{
			mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			mousePos.z=0;
			if (!pointsList.Contains (mousePos)) 
			{
				pointsList.Add (mousePos);
				line.SetVertexCount (pointsList.Count);
				line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
				if(isLineCollide())
				{
					isMousePressed = false;
					line.SetColors(Color.red, Color.red);

					//proses Algoritma
					simpanConvex = new List<Vector3>();
					simpanConvex = convex_hull(pointsList);
					pointsList.Add (new Vector3(2.23f,-0.89f,0));
					convexPlus = new List<Vector3>();
					convexPlus = convex_hull(pointsList);

					//proses cek objek di dalam atau di luar
					for (int i = 0; i < convexPlus.Count; i++)
					{
						if ((simpanConvex<em>.x == convexPlus<em>.x) && (simpanConvex_.y == convexPlus*.y))*_</em></em>

* {*
* Debug.Log (“Didalam”);*
* }*
* else*
* {*
* Debug.Log (“Diluar”);*
* }*
* }*
* }*
* }*
* }*
* }*
* // ----------------------------------- *
* // Following method checks is currentLine(line drawn by last two points) collided with line*
* // ----------------------------------- *
* private bool isLineCollide()*
* {*
* if (pointsList.Count < 2)*
* return false;*
* int TotalLines = pointsList.Count - 1;*
* myLine[] lines = new myLine[TotalLines];*
* if (TotalLines > 1)*
* {*
* for (int i=0; i<TotalLines; i++)*
* {*
lines .StartPoint = (Vector3)pointsList ;
_ lines .EndPoint = (Vector3)pointsList [i + 1];
* }
}
for (int i=0; i<TotalLines-1; i++)
{
myLine currentLine;
currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];
currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];
if (isLinesIntersect (lines , currentLine))
return true;
}
return false;
}
// -----------------------------------
// Following method checks whether given two points are same or not*

* // -----------------------------------
private bool checkPoints (Vector3 pointA, Vector3 pointB)
{
return (pointA.x == pointB.x && pointA.y == pointB.y);
}
// -----------------------------------
// Following method checks whether given two line intersect or not*

* // -----------------------------------
private bool isLinesIntersect (myLine L1, myLine L2)
{
if (checkPoints (L1.StartPoint, L2.StartPoint) ||
checkPoints (L1.StartPoint, L2.EndPoint) ||
checkPoints (L1.EndPoint, L2.StartPoint) ||
checkPoints (L1.EndPoint, L2.EndPoint))
return false;*_

* return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&*
* (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) &&*
* (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&*
* (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y))*
* );*
* }*

* //fungsi cross algoritma*
* private double cross (Vector3 o, Vector3 a, Vector3 b)*
* {*
_ return ((a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x));
* }*_

* //fungsi sort*
* private int Vector3Compare (Vector3 v1, Vector3 v2)*
* {*
* if (v1.x < v2.x) {*
* return -1;*
* } else if (v1.x == v2.x) {*
* if (v1.y < v2.y) {*
* return -1;*
* } else if (v1.y == v2.y) {*
* if (v1.z < v2.z) {*
* return -1;*
* } else if (v1.z == v2.z) {*
* return 0;*
* } else {*
* return 1;*
* }*
* } else {*
* return 1;*
* }*
* } else {*
* return 1;*
* }*
* }*

* //fungsi Algoritma*
* private List convex_hull (List listCome)
_ {
int n = listCome.Count;
int k = 0;
H = new List();
//sort*

* //listCome.Sort ();
//listCome = listCome.OrderBy (v => v.x).OrderByDescending (v => v.y);
listCome.Sort (Vector3Compare);
//build lower hull*

* for (int i = 0; i < n; i++) {
while (k >= 2 && cross(H[k-2], H[k-1], listCome) <= 0) k–;
H[k++] = listCome;
}*_

* //build upper hull*
* for (int i = n-2, t = k+1; i >= 0; i–) {*
_ while (k >= t && cross(H[k-2], H[k-1], listCome*) <= 0) k–;
H[k++] = listCome;
}
//resize dipertanyakan*

* //H.Capacity.Equals (k);*_

* return H;*
* }*

}

Thank you very much for the answer, Sergio and Bunny83. Actually, I’m trying to move code monotone chain convex hull from C++ to the C# unity, and H[k++] is work in C++. And in C++ the inisialize array of H have size [2*n] . But, I read that size of list in C# is can’t decided in inisialize. How can I repair this ? Any suggest ?

And sorry if my english is bad >< . I’m not english anymore