- Home /
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[i].x == convexPlus[i].x) && (simpanConvex[i].y == convexPlus[i].y))
{
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 [i].StartPoint = (Vector3)pointsList [i];
lines [i].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 [i], 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<Vector3> convex_hull (List<Vector3> listCome)
{
int n = listCome.Count;
int k = 0;
H = new List<Vector3>();
//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[i]) <= 0) k--;
H[k++] = listCome[i];
}
//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[i]) <= 0) k--;
H[k++] = listCome[i];
}
//resize dipertanyakan
//H.Capacity.Equals (k);
return H;
}
}
the problem is k value, use a Debug.Log(k);
to see the value of k before the H[k++] = listCome[i];
on line 184. In a List you can't call List[index] where the index is greater or equal than the List Count value or the index is less than 0.
Edit: C# list can only use index less than List Count, if you want use a index greater than count you need use a array, but using a array you will need specify the array size when you create the array, and you will need check if the index is less than array Length before set the value to index, if the index is equal or greater than Length you can use Array.Resize to change array Length.
Right, the list "H" is empty so you can't access a specific element as it doesn't contain any elements.
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
@samyn26 C# list can only use index less than List Count, if you want use a index greater than count you need use a array, but using a array you will need specify the array size when you create the array, and you will need check if the index is less than array Length before set the value to index, if the index is equal or greater than Length you can use Array.Resize to change array Length.
Answer by samyn26 · Nov 11, 2016 at 08:33 AM
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
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List Works But Still Get Argument Out of Range Exception 1 Answer
Why does List
JSON deserialization 0 Answers