private DividingVert LineIntersectPoint(Vector3 v1, Vector3 v2, Vector3 point) { Vector2 A = new Vector2(v1.X, v1.Z); Vector2 B = new Vector2(v2.X, v2.Z); Vector2 p = new Vector2(point.X, point.Z); DividingVert edgePoint = new DividingVert(); //get the normalized line segment vector Vector2 v = B - A; v.Normalize(); //determine the point on the line segment nearest to the point p float distanceAlongLine = Vector2.Dot(p, v) - Vector2.Dot(A, v); Vector2 nearestPoint; if (distanceAlongLine < 0) { //closest point is A nearestPoint = A; } else if (distanceAlongLine > Vector2.Distance(A, B)) { //closest point is B nearestPoint = B; } else { //closest point is between A and B... A + d * ( ||B-A|| ) nearestPoint = A + distanceAlongLine * v; } //Calculate the distance between the two points float actualDistance = Vector2.Distance(nearestPoint, p); edgePoint.distance = actualDistance; edgePoint.big = edgePoint.small = -1; edgePoint.position = new Vector3(nearestPoint.X, 0, nearestPoint.Y); return edgePoint; }
public void Divide(DividingVert first, DividingVert second, out Board partOne, out Board partTwo) { // /* ADD TEST TO FIND IF NOT ON THE SAME EDGE*/ // Board part1 = new Board(texture,effect); Board part2 = new Board(texture, effect); int p1_pNum, p2_pNum; p1_pNum = Math.Abs(first.big - second.small) + 3; if ((first.big == vertNum - 1) && (second.small == 0)) p1_pNum -= 2; p2_pNum = indices.Length - p1_pNum + 2; Vector3[] p1_points = new Vector3[p1_pNum]; Vector2[] p1_texCords = new Vector2[p1_pNum]; p1_points[0] = first.position; p1_texCords[0] = findTexCords(first); p1_points[p1_pNum - 1] = second.position; p1_texCords[p1_pNum - 1] = findTexCords(second); for (int i = 0; i < p1_pNum - 2; i++) { p1_points[i + 1] = vertices[(first.big + i) % vertNum].Position; p1_texCords[i + 1] = vertices[(first.big + i) % vertNum].TextureCoordinate; } Vector3[] p2_points = new Vector3[p2_pNum]; Vector2[] p2_texCords = new Vector2[p2_pNum]; p2_points[0] = second.position; p2_texCords[0] = p1_texCords[p1_pNum - 1]; //alreadt calculated before a moment p2_points[p2_pNum - 1] = first.position; p2_texCords[p2_pNum - 1] = p1_texCords[0]; //alreadt calculated before a moment for (int i = 0; i < p2_pNum - 2; i++) { p2_points[i + 1] = vertices[(second.big + i) % vertNum].Position; p2_texCords[i + 1] = vertices[(second.big + i) % vertNum].TextureCoordinate; } part1.Initialize(p1_pNum, p1_points, p1_texCords); part2.Initialize(p2_pNum, p2_points, p2_texCords); // storing the smaller part in parOne , bigger -> partTwo if (Vector3.Distance(center,part1.center) >= Vector3.Distance(center,part2.center)) { partOne = part1; partTwo = part2; } else { partOne = part2; partTwo = part1; } }
private Vector2 findTexCords(DividingVert divVert) { float smallX = vertices[divVert.small].Position.X; float smallCordX = vertices[divVert.small].TextureCoordinate.X; float bigX = vertices[divVert.big].Position.X; float bigCordX = vertices[divVert.big].TextureCoordinate.X; float vertX = divVert.position.X; float smallZ = vertices[divVert.small].Position.Z; float smallCordZ = vertices[divVert.small].TextureCoordinate.Y; float bigZ = vertices[divVert.big].Position.Z; float bigCordZ = vertices[divVert.big].TextureCoordinate.Y; float vertZ = divVert.position.Z; float xSize = Math.Abs(bigX - smallX); float zSize = Math.Abs(bigZ - smallZ); //the point from wich you maser the distance float relativeX = (bigCordX < smallCordX ? bigX : smallX); float relativeZ = (bigCordZ < smallCordZ ? bigZ : smallZ); float xRel = (xSize != 0 ? Math.Abs(relativeX - vertX) / xSize : smallX); float zRel = (zSize != 0 ? Math.Abs(relativeZ - vertZ) / zSize : smallZ); float xCsize = Math.Abs(bigCordX - smallCordX); float zCsize = Math.Abs(bigCordZ - smallCordZ); float xCrel = (xCsize != 0 ? xRel * xCsize : smallCordX); float zCrel = (zCsize != 0 ? zRel * zCsize : smallCordZ); return new Vector2(xCrel, zCrel); }
private Vector2 findTexCords(DividingVert divVert) { float smallX = vertices[divVert.small].Position.X; float smallCordX = vertices[divVert.small].TextureCoordinate.X; float bigX = vertices[divVert.big].Position.X; float bigCordX = vertices[divVert.big].TextureCoordinate.X; float vertX = divVert.position.X; float smallZ = vertices[divVert.small].Position.Z; float smallCordZ = vertices[divVert.small].TextureCoordinate.Y; float bigZ = vertices[divVert.big].Position.Z; float bigCordZ = vertices[divVert.big].TextureCoordinate.Y; float vertZ = divVert.position.Z; float xSize = Math.Abs(bigX - smallX); float zSize = Math.Abs(bigZ - smallZ); float xRel = (xSize != 0 ? Math.Abs(Math.Min(smallX, bigX) - vertX) / xSize : smallX); float zRel = (zSize != 0 ? Math.Abs(Math.Min(smallZ, bigZ) - vertZ) / zSize : smallZ); float xCsize = Math.Abs(bigCordX - smallCordX); float zCsize = Math.Abs(bigCordZ - smallCordZ); float xCrel = (xCsize != 0 ? xRel * xCsize : smallCordX); float zCrel = (zCsize != 0 ? zRel * zCsize : smallCordZ); return new Vector2(xCrel, zCrel); //Vector2 distFromSmall = new Vector2(Math.Abs(divVert.position.X - vertices[divVert.small].Position.X), // Math.Abs(divVert.position.Z - vertices[divVert.small].Position.Z)); //Vector2 sizeOfLine = new Vector2(Math.Abs(vertices[divVert.small].Position.X - vertices[divVert.big].Position.X), // Math.Abs(vertices[divVert.small].Position.Z - vertices[divVert.big].Position.Z)); //distFromSmall /= new Vector2((sizeOfLine.X != 0 ? sizeOfLine.X : 1), (sizeOfLine.Y != 0 ? sizeOfLine.Y : 1)); //sizeOfLine = new Vector2(Math.Abs(vertices[divVert.small].TextureCoordinate.X - // vertices[divVert.big].TextureCoordinate.X), // Math.Abs(vertices[divVert.small].TextureCoordinate.Y - vertices[divVert.big].TextureCoordinate.Y)); //distFromSmall *= sizeOfLine; //if (divVert.position.X == vertices[divVert.small].TextureCoordinate.X) // distFromSmall.X = vertices[divVert.small].TextureCoordinate.X; //if (vertices[divVert.small].TextureCoordinate.Y == vertices[divVert.big].TextureCoordinate.Y) // distFromSmall.Y = vertices[divVert.small].TextureCoordinate.Y; //return distFromSmall; }