Lets say a=x+y and b=2x-y and I want to plot a-b=0. I can manually rewrite this from (x+y)-(2x-y)=0 to y=.5x which makes it trivial to graph, but how do I rewrite it in code? Given x how do I calculate y if all I have is (x+y)-(2x-y)=0?
To be more specific, I am trying to graph the decision boundary of a neural network. I want to be able to change the layers and outputs at will, essentially changing the function I get as an output.
This is an example of an output I could get:
(x_1 w_2 + x_2 w_2 + b_1) w_7
+ (x_1 w_3 + x_2 w_4 + b_2) w_8
+ (x_1 w_5 + x_2 w_6 + b_3) w_9
+ b_4 (x_1 w_1 + x_2 w_2 + b_1) w_10
+ (x_1 w_3 + x_2 w_4 + b_2) w_11
+ (x_1 w_5 + x_2 w_6 + b_3) w_12
+ b_5
It's a 1 by 2 matrix and I know all values except x2 which is the y-axis. In order to draw the decision boundary I have to calculate a-b=0 where a and b both contain x and y. I can manually separate y to get y=... ,but that's not an option if the results in the output matrix change. How do I seperate/calculate the y?
I am using c# in Unity and passing the points on the graph into the LineRenderer.
Alright, I found the solution the same day of posting the question but had already been messing about for days. It turned out to be a math question after all.
Here's a link to the specific setup for the neural network using a linear activation: https://www.desmos.com/calculator/crmeebqnfb
I manually rewrote the matrix multiplication for this specific setup into a function and was looking for a way to do that for any size and number of invisible layers.
The solution I found is to separate the input matrix into x1 and x2 and separately do the matrix multiplication for them. The x1 value gets the biases added but the x2 doesn't and the first weight matrix has to be split in 2 so x1 can get multiplied with the first row and x2 with the second row.
If you then do the matrix multiplication from there you'll get two 2 matrices like this:
[firstx1answer secondx1answer] [firstx2answer secondx2answer]
And then you can put them into this function:
Edit for better clarification:
Maybe a bit confusing but here's my code. CreateDecisionBoundaryPoints is where this is implemented:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.IO;
using System;
[ExecuteAlways]
public class Controller : MonoBehaviour
{
public Transform LineRenderer;
public GameObject textPrefab;
public GameObject pointPrefab;
public Transform weightsUI;
public Transform biasesUI;
[Range(.001f, .1f)] public float delta;
public int numberOfHiddenLayers;
public bool debugWeightMatrices;
public bool debugBiasMatrices;
[HideInInspector] public string[] dataValues;
private void Start()
{
if (Application.isPlaying)
{
//read file
int numberOfLines;
dataValues = ReadData("D:\\Documents\\Unity Projects\\Learning Machine Learning\\Assets\\Data.csv", out numberOfLines);
int numOfOutputNeurons = CreatePointsUI(numberOfLines, dataValues);
//create layerSizes for example [2,3,2]
int[] layerSizes = new int[numberOfHiddenLayers + 2];
layerSizes[0] = (dataValues.Length / numberOfLines) - 1;
layerSizes[numberOfHiddenLayers + 1] = numOfOutputNeurons;
for (int i = 0; i < numberOfHiddenLayers; i++)
{
layerSizes[i+1] = Mathf.Max((dataValues.Length / numberOfLines) - 1, numOfOutputNeurons) + 1;
}
//create the actual matrices
List<float[,]> weights = new List<float[,]>();
List<float[]> biases = new List<float[]>();
MakeTheMatrices(layerSizes, out weights, out biases);
//fill weights with random values
RandomlyFillMatrices(weights);
//print matrices to make sure they're the right size and filled randomly
if (debugWeightMatrices)
Debug.Log(PrintMatrices(weights, "Weight Matrices"));
if (debugBiasMatrices)
Debug.Log(PrintMatrices(biases, "Bias Matrices"));
LineRenderer.GetComponent<DrawDecisionBoundary>().DrawLine(CreateDecisionBoundaryPoints(weights, biases, delta));
}
}
public struct OutputNeuronsAndColours
{
public string value;
public Color color;
public OutputNeuronsAndColours(string value, Color color)
{
this.value = value;
this.color = color;
}
}
public void DoTheWeightsStufUI(int weights)
{
int cwn = 0;
List<Transform> ws = new List<Transform>();
foreach (Transform child in weightsUI)
{
cwn++;
ws.Add(child);
}
int wta = weights - cwn;
for (int i = wta; i < 0; i++)
{
cwn--;
DestroyImmediate(ws[cwn].gameObject);
ws.RemoveAt(cwn);
}
for (int i = wta; i > 0; i--)
{
cwn++;
GameObject weight = Instantiate(textPrefab, weightsUI);
weight.GetComponentInChildren<TMP_Text>().SetText("W" + cwn.ToString());
}
}
public void DoTheBiasesStufUI(int biases)
{
int cbn = 0;
List<Transform> bs = new List<Transform>();
foreach (Transform child in biasesUI)
{
cbn++;
bs.Add(child);
}
int bta = biases - cbn;
for (int i = bta; i < 0; i++)
{
cbn--;
DestroyImmediate(bs[cbn].gameObject);
bs.RemoveAt(cbn);
}
for (int i = bta; i > 0; i--)
{
cbn++;
GameObject bias = Instantiate(textPrefab, biasesUI);
bias.GetComponentInChildren<TMP_Text>().SetText("B" + cbn.ToString());
}
}
string[] ReadData(string path, out int numberOfLines)
{
List<string> data_values = new List<string>();
StreamReader strReader = new StreamReader(path);
bool endOfFile = false;
int numOfLines = 0;
while (!endOfFile)
{
string data_string = strReader.ReadLine();
if (data_string == null)
{
endOfFile = true;
break;
}
else
numOfLines += 1;
data_values.AddRange(data_string.Split(','));
}
numberOfLines = numOfLines;
return data_values.ToArray();
}
int CreatePointsUI(int numberOfLines, string[] dataValues)
{
string[] possibleOutputs = new string[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
possibleOutputs[i] = dataValues[(i * (dataValues.Length / numberOfLines)) + ((dataValues.Length / numberOfLines) - 1)];
}
List<OutputNeuronsAndColours> outputNeurons = new List<OutputNeuronsAndColours>(possibleOutputs.Length);
for (int i = 0; i < possibleOutputs.Length; i++)
{
bool contains = false;
for (int x = 0; x < outputNeurons.Count; x++)
{
if (possibleOutputs[i] == outputNeurons[x].value)
contains = true;
}
if (!contains)
outputNeurons.Add(new OutputNeuronsAndColours(possibleOutputs[i], new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f))));
}
for (int i = 0; i < numberOfLines; i++)
{
GameObject point = Instantiate(pointPrefab);
point.transform.position = new Vector2(float.Parse(dataValues[i * (dataValues.Length / numberOfLines)]), float.Parse(dataValues[(i * (dataValues.Length / numberOfLines)) + 1]));
foreach (OutputNeuronsAndColours value in outputNeurons)
{
if (value.value == dataValues[(i * (dataValues.Length / numberOfLines)) + ((dataValues.Length / numberOfLines) - 1)])
point.GetComponent<SpriteRenderer>().color = value.color;
}
}
return outputNeurons.Count;
}
public static void MakeTheMatrices(int[] layerSizes, out List<float[,]> weights, out List<float[]> biases)
{
List<float[,]> tempWeights = new List<float[,]>();
List<float[]> tempBiases = new List<float[]>();
for (int i = 0; i < layerSizes.Length - 1; i++)
{
tempWeights.Add(new float[layerSizes[i], layerSizes[i + 1]]);
}
for (int i = 1; i < layerSizes.Length; i++)
{
List<float> temp = new List<float>();
for (int x = 0; x < layerSizes[i]; x++)
temp.Add(0);
tempBiases.Add(temp.ToArray());
}
weights = tempWeights;
biases = tempBiases;
}
public static void RandomlyFillMatrices(List<float[,]> matrices)
{
foreach (float[,] matrix in matrices)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int x = 0; x < matrix.GetLength(1); x++)
{
matrix[i, x] = UnityEngine.Random.Range(-3f, 3f);
}
}
}
}
public static string PrintMatrices(List<float[,]> matrices, string name = "Count")
{
string returnString = matrices.Count + " " + name;
foreach (float[,] matrix in matrices)
{
returnString += " (" + matrix.GetLength(0) + ", " + matrix.GetLength(1) + ")";
for (int i = 0; i < matrix.GetLength(0); i++)
{
string log = "";
if (i == 0)
log += "[";
else
log += " ";
for (int x = 0; x < matrix.GetLength(1); x++)
{
log += matrix[i, x];
if(x != matrix.GetLength(1) - 1)
log += " ";
}
if (i == matrix.GetLength(0) - 1)
log += "]";
Debug.Log(log);
}
}
return returnString;
}
public static string PrintMatrices(List<float[]> matrices, string name = "Count")
{
string returnString = matrices.Count + " " + name;
foreach (float[] matrix in matrices)
{
returnString += " (" + matrix.Length + ")";
string log = "[";
for (int i = 0; i < matrix.Length; i++)
{
log += matrix[i];
if (i != matrix.Length - 1)
log += " ";
}
log += "]";
Debug.Log(log);
}
return returnString;
}
private Vector3[] CreateDecisionBoundaryPoints(List<float[,]> weights, List<float[]> biases, float delta)
{
//check whether there are exactly 2 input neurons
if (weights[0].GetLength(0) != 2)
Debug.LogError("Not exactly 2 input neurons!");
//check whether there are exactly 2 output neurons
if (biases[biases.Count - 1].Length != 2)
Debug.LogError("Not exactly 2 output neurons!");
//create the values for the first layer
float[] weightsForFirstLayerX = new float[weights[0].GetLength(1)];
for (int i = 0; i < weights[0].GetLength(1); i++)
{
weightsForFirstLayerX[i] = weights[0][0, i];
}
float[] denominatorValuesFirstLayer = new float[weights[0].GetLength(1)];
for (int i = 0; i < weights[0].GetLength(1); i++)
{
denominatorValuesFirstLayer[i] = weights[0][1, i];
}
List<Vector3> pointsForGraph = new List<Vector3>();
//Calculate the y value(s) for each x with interval delta
for (float x = -.04f; x <= 1 + delta; x += delta)
{
float[] numeratorValuesFirstLayer = new float[weightsForFirstLayerX.Length];
for (int i = 0; i < weightsForFirstLayerX.Length; i++)
numeratorValuesFirstLayer[i] = x * weightsForFirstLayerX[i] + biases[0][i];
//get the row matrices for the decision boundary function
float[] numeratorResults = PassValuesThroughMatrices(numeratorValuesFirstLayer, weights, biases, true);
float[] denominatorResults = PassValuesThroughMatrices(denominatorValuesFirstLayer, weights, biases, false);
float y = (numeratorResults[1] - numeratorResults[0]) / (denominatorResults[0] - denominatorResults[1]);
pointsForGraph.Add(new Vector3(x, y, -1));
}
return pointsForGraph.ToArray();
}
private float[] PassValuesThroughMatrices(float[] values, List<float[,]> weights, List<float[]> biases, bool isNumerator)
{
float[] previousLayer = values;
//loop passing the previous layer values through the current layer: values = values * weights + biases
for (int i = 1; i < weights.Count; i++)
{
float[] temp = new float[weights[i].GetLength(1)];
//loop through the colums in the weight matrix
for (int v = 0; v < weights[i].GetLength(1); v++)
{
float value = 0;
//loop through the rows in the weight matrix
for (int b = 0; b < weights[i].GetLength(0); b++)
value += previousLayer[b] * weights[i][b, v];
if (isNumerator)
value += biases[i][v];
temp[v] = value;
}
previousLayer = temp;
}
//return the last itteration of values
return previousLayer;
}
}
Related
So I'm trying to sort a list of books that have been added to an array alphabetically however, whenever I input the third book, the list flips and sorts the list in unalphabetical order.
If anyone knows why this is, please comment and let me know, my code is below.
The sort to determine if two indexes need to be swapped
private void bookSort()
{
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < bookPTR - 1; x++)
{
if (string.Compare(books[x].GStitle, books[x + 1].GStitle) > 0)
{
bookSwapRoutine(books[x]);
}
}
}
}
The swap itself
private void bookSwapRoutine(Book book, int x = 0)
{
string tempString = books[x].GStitle;
books[x].GStitle = books[x + 1].GStitle;
books[x + 1].GStitle = tempString;
int tempInt = books[x].GSisbn;
books[x].GSisbn = books[x + 1].GSisbn;
books[x + 1].GSisbn = tempInt;
tempString = books[x].GSauthor;
books[x].GSauthor = books[x + 1].GSauthor;
books[x + 1].GSauthor = tempString;
tempString = books[x].GSpublisher;
books[x].GSpublisher = books[x + 1].GSpublisher;
books[x + 1].GSpublisher = tempString;
double tempDouble = books[x].GSprice;
books[x].GSprice = books[x + 1].GSprice;
books[x + 1].GSprice = tempDouble;
tempString = books[x].GSdate;
books[x].GSdate = books[x + 1].GSdate;
books[x + 1].GSdate = tempString;
}
Because of this place. That function call always swap books at zero index and first index because of default parameter x = 0.
bookSwapRoutine(books[x]);
You should call it like.
bookSwapRoutine(books[x], x);
This will swap books[x] and books[x + 1] for you.
If you want just sort your books by GStitle in alphabeticall order you can call.
Array.Sort(books, (x, y) => string.Compare(x.GStitle, y.GStitle, StringComparison.InvariantCulture));
Here all code, with correct bubble sort, if it will help you.
public static void Main()
{
var books = new Book[]
{
new Book() {GStitle = "E"},
new Book() {GStitle = "D"},
new Book() {GStitle = "C"},
new Book() {GStitle = "B"},
new Book() {GStitle = "A"}
};
Console.WriteLine("Before sort.");
foreach (var book in books)
{
Console.WriteLine(book.GStitle);
}
Array.Sort(books, (x, y) => string.Compare(x.GStitle, y.GStitle, StringComparison.InvariantCulture));
//BookSort(books);
Console.WriteLine("After sort.");
foreach (var book in books)
{
Console.WriteLine(book.GStitle);
}
}
public class Book
{
public string GStitle { get; set; }
}
public static void BookSort(Book[] books)
{
for (int y = 0; y < books.Length; y++)
{
for (int x = 0; x < books.Length - 1 - y; x++)
{
if (string.Compare(books[x].GStitle, books[x + 1].GStitle, StringComparison.InvariantCulture) > 0)
{
var temp = books[x];
books[x] = books[x + 1];
books[x + 1] = temp;
}
}
}
}
I created a class that I called "Descripteurs". I want to call this class to create a csv file. So I have called my list of descriptors created in the class " Descriptors ".
I can't not associate my positions of pixel with descriptors to store it in my csv.
I should have with every pixel in my image the descriptors in the class " Descripteurs"(moyenne, number of black pixel, number of white pixel.....). The csv should have the position in each pixel and all the descriptors calculated in the class "Descripteurs ". I don't know if there is a thread problem or not.
My function is here:
public bool StoreIntoCsv(string pathOriginalPicture, string pathOriginalPictureWithOnlyOneLayer, int classe, BackgroundWorker worker, DoWorkEventArgs e)
{
//preprocessing of the original picture and the original picture with only one layer
Descripteurs featuresPathOriginal = new Descripteurs(new Bitmap(pathOriginalPicture));// m_constructeur de la classe Descripteurs
Bitmap binaryOriginalPictureWithOnlyOneLayer = new Bitmap(pathOriginalPictureWithOnlyOneLayer); // correspond à l'image binaire avec le L.
//we want to get the class of the picture
string[] res = pathOriginalPicture.Split('\\');
string classeName = res[res.Count() - 2]; // count = compter
//retrieving the file path of the csv
string pathFolder = pathOriginalPicture.Split('.').First();// le split divise la chaine en sous chaine en fonction des caractères
pathFolder = pathFolder.Remove(pathFolder.LastIndexOf('\\'));
string pathCsv = pathFolder + "\\" + classeName + ".csv";
libaforge.AForge.IntPoint pixel = new libaforge.AForge.IntPoint();
//open the stream
using (StreamWriter stream = File.AppendText(pathCsv)) //streamwriter permet d'écrire dans un fichier
{
//browse all the picture
for (int x = 0; x < binaryOriginalPictureWithOnlyOneLayer.Width; x = x + 2)
{
for (int y = 0; y < binaryOriginalPictureWithOnlyOneLayer.Height; y = y + 2)
{
//the user stop the application
if (worker.CancellationPending)//checks for cancel request
{
e.Cancel = true;
return false;
}
//we know, where is it the pixel black on the data set training
if (binaryOriginalPictureWithOnlyOneLayer.GetPixel(x, y).ToArgb() == Color.Black.ToArgb())
{
pixel.X = x;
pixel.Y = y;
WriteLineToCsv(pixel, featuresPathOriginal.Extract_Desscripteurs(pixel), classe, stream);
}
}
}
return true;
}
}
My class "Descripteurs is here:
namespace ProjetPRD
{
extern alias libaforge;
public class Descripteurs
{
//private Bitmap _imageLidar;
private static Bitmap image;
public Descripteurs(Bitmap img)
{
image = img;
}
//public static List <double> Extract_Desscripteurs(Bitmap image)
//public static List <double> Extract_Desscripteurs(libaforge.AForge.IntPoint pixel)
public double[] Extract_Desscripteurs(libaforge.AForge.IntPoint pixel)
{
int pixel_Central = 0;
double Moy2_Haut_Gauche = 0;
double Moy3_Haut_Droite = 0;
double Moy4_Bas_Gauche = 0;
double Moy5_Bas_Droite = 0;
int Difference = 0; // pour calculer la difference entre la valeur max et la valeur min de notre masc
int Nb_PNoir_Haut_Gauche = 0;
int Nb_PBlanc_Haut_Gauche = 0;
int Nb_PGris_Haut_Gauche = 0;
int Nb_PNoir_Haut_Droite = 0;
int Nb_PBlanc_Haut_Droite = 0;
int Nb_PGris_Haut_Droite = 0;
int Nb_PNoir_Bas_Gauche = 0;
int Nb_PBlanc_Bas_Gauche = 0;
int Nb_PGris_Bas_Gauche = 0;
int Nb_PNoir_Bas_Droite = 0;
int Nb_PBlanc_Bas_Droite = 0;
int Nb_PGris_Bas_Droite = 0;
List<double> caracteristique = new List <double>();
lock (image )
{
BitmapData bmd = new BitmapData();
try
{
Rectangle Rect = new Rectangle(0, 0, image.Width, image.Height);
bmd = image.LockBits(Rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
//System.Drawing.Imaging.BitmapData bmpData = bmd.LockBits(Rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,bmd.PixelFormat);
unsafe
{
byte* Ptr = (byte*)bmd.Scan0;
for (int j = 0; j < bmd.Height; j++)
{
for (int i = 0; i < bmd.Width; i++)
{
//initialisation des différents éléments du masque 3*3
int couleur1 = Ptr[j * bmd.Width + i];
int couleur2 = Ptr[j * bmd.Width + (i + 1)];
int couleur3 = Ptr[j * bmd.Width + (i + 2)];
int couleur4 = Ptr[(j + 1) * bmd.Width + i];
pixel_Central = Ptr[(j + 1) * bmd.Width + (i + 1)];
int couleur6 = Ptr[(j + 1) * bmd.Width + (i + 2)];
int couleur7 = Ptr[(j + 2) * bmd.Width + (i)];
int couleur8 = Ptr[(j + 2) * bmd.Width + (i + 1)];
int couleur9 = Ptr[(j + 2) * bmd.Width + (i + 2)];
//faire la moyenne de chaque bloc de 4
Moy2_Haut_Gauche = (couleur1 + couleur2 + couleur4 + pixel_Central) / 4;
Moy3_Haut_Droite = (couleur2 + couleur3 + pixel_Central + couleur6) / 4;
Moy4_Bas_Gauche = (couleur4 + pixel_Central + couleur7 + couleur8) / 4;
Moy5_Bas_Droite = (pixel_Central + couleur6 + couleur8 + couleur9) / 4;
//remplir la liste des caractéristiques
caracteristique.Add(pixel_Central);
caracteristique.Add(Moy2_Haut_Gauche);
caracteristique.Add(Moy3_Haut_Droite);
caracteristique.Add(Moy4_Bas_Gauche);
caracteristique.Add(Moy5_Bas_Droite);
int[] tab_Difference = { couleur1, couleur2, couleur3, couleur4, pixel_Central, couleur6, couleur7, couleur8, couleur9 };
Difference = tab_Difference.Max() - tab_Difference.Min();
int[] tab = { couleur1, couleur2, couleur4, pixel_Central };
for (int k = 0; k < tab.Length; k++)
{
if (tab[k] < 60)
{
Nb_PNoir_Haut_Gauche++;
}
else
{
if (tab[k] > 180)
{
Nb_PBlanc_Haut_Gauche++;
}
else
{
Nb_PGris_Haut_Gauche++;
}
}
}
int[] tab2 = { couleur2, couleur3, pixel_Central, couleur6 };
for (int m = 0; m < tab2.Length; m++)
{
if (tab2[m] < 60)
{
Nb_PNoir_Haut_Droite++;
}
else
{
if (tab2[m] > 180)
{
Nb_PBlanc_Haut_Droite++;
}
else
{
Nb_PGris_Haut_Droite++;
}
}
}
int[] tab3 = { couleur4, pixel_Central, couleur7, couleur8 };
for (int n = 0; n < tab3.Length; n++)
{
if (tab3[n] < 60)
{
Nb_PNoir_Bas_Gauche++;
}
else
{
if (tab3[n] > 180)
{
Nb_PBlanc_Bas_Gauche++;
}
else
{
Nb_PGris_Bas_Gauche++;
}
}
}
int[] tab4 = { pixel_Central, couleur6, couleur8, couleur9 };
for (int n = 0; n < tab4.Length; n++)
{
if (tab4[n] < 60)
{
Nb_PNoir_Bas_Droite++;
}
else
{
if (tab4[n] > 180)
{
Nb_PBlanc_Bas_Droite++;
}
else
{
Nb_PGris_Bas_Droite++;
}
}
}
caracteristique.Add(Difference);
caracteristique.Add(Nb_PNoir_Haut_Gauche);
caracteristique.Add(Nb_PNoir_Haut_Droite);
caracteristique.Add(Nb_PNoir_Bas_Gauche);
caracteristique.Add(Nb_PNoir_Bas_Droite);
caracteristique.Add(Nb_PBlanc_Haut_Gauche);
caracteristique.Add(Nb_PBlanc_Haut_Droite);
caracteristique.Add(Nb_PBlanc_Bas_Gauche);
caracteristique.Add(Nb_PBlanc_Bas_Droite);
caracteristique.Add(Nb_PGris_Haut_Gauche);
caracteristique.Add(Nb_PGris_Haut_Droite);
caracteristique.Add(Nb_PGris_Bas_Gauche);
caracteristique.Add(Nb_PGris_Bas_Droite);
}
}
//mesCaracteristiques = caracteristique.ToArray();
}
}
finally
{
image.UnlockBits(bmd);
}
//e.Graphics.DrawImage(bmd, 0, 150);
//return mesCaracteristiques;
return caracteristique.ToArray();
}
}
}
}
I don't know if i was clear with my explication but i really need help.
I have unity script which will instantiate objects during the runtime. I need to add a texture to my prefab. I went through some solutions and i couldnt find a proper solution. my code is
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class createWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
int k=0;
int count = 0;
public TextAsset TextFile;
public GameObject wallPrehab;
GameObject wall;
public Collider coll;
public Camera c1;
public Camera c2;
public Manager mgr;
/*
*even we though it get as x,y values when modeling convert it's y value as z
*/
Vector3[] coordinatesX = null;
Vector3[] coordinatesY = null;
public Texture texture;
/*
* Use this for initialization
*/
void Start () {
readTextFileLines();
Vector3 start = new Vector3();
Vector3 end = new Vector3();
for (int i = 0; i < coordinatesX.Length; i++)
{
k = i + 1;
if (k != coordinatesX.Length)
{
if(coordinatesX[i].x == coordinatesX[k].x){
start = coordinatesX[i];
end = coordinatesX[k];
setStart(start);
setEnd(end);
adjust();
}
}
}
for(int j = 0; j < coordinatesY.Length; j++){
k = j + 1;
if (k != coordinatesY.Length)
{
if(coordinatesY[j].z == coordinatesY[k].z){
start = coordinatesY[j];
end = coordinatesY[k];
setStart(start);
setEnd(end);
adjust();
}
}
}
}
// Update is called once per frame
void Update () {
//getinput();
}
/*
* getting the mouse clicked position coordinate
*/
int number=0;
void setStart(Vector3 x){
creating = true;
start.transform.position = x;
wall = Instantiate (wallPrehab, start.transform.position, Quaternion.identity)as GameObject;
wall.GetComponent<WallScript>().mgr=mgr;
wall.gameObject.name = number.ToString ();
number++;
}
/*
* getting the mouse click over position coordinate
*/
void setEnd(Vector3 y){
end.transform.position = y;
}
/*
* invoking the wall building method
*/
void adjust(){
adjustWall ();
}
/*
* build the wall in between start point and the end point
*/
void adjustWall(){
start.transform.LookAt (end.transform.position);
end.transform.LookAt (start.transform.position);
float distance = Vector3.Distance (start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
}
/*
* Reading from the text file
*/
void readTextFileLines()
{
int count = 0;
string splits = TextFile.text.TrimStart ('[');
string[] split = TextFile.text.Split (')');
string split_1 = null;
string split_2 = null;
string split_3 = null;
int pos = 0;
int lengthOfString = 0;
int valX, valZ, valX1, valZ1 = 0;
/*
* Getting the count of the coordinates in the array
*/
foreach (string x in split) {
count++;
}
string[] stringArr = new string[count];
int[] xSortX = new int[count];
int[] xSortZ = new int[count];
int[] ySortX = new int[count];
int[] ySortZ = new int[count];
/*
* Splitting the coordinates as x,y and store in an array
*/
foreach (string coord in split) {
split_1 = coord;
split_1 = split_1.Trim ('[');
split_1 = split_1.Trim ('(');
split_1 = split_1.Trim (',');
split_1 = split_1.Trim (' ');
split_1 = split_1.Trim ('(');
split_1 = split_1.TrimEnd (']');
stringArr [pos] = split_1;
pos++;
}
//Debug.Log("Array Length " + stringArr.Length);
/*
* extracting simalar x coordinates
*/
//Debug.Log("");
//Debug.Log("-----------------extracting simalar x coordinates----------------");
//Debug.Log("");
int indexX = 0;
int loopRunX = 0;
for (int a = 0; a < stringArr.Length - 1; a = indexX) {
split_2 = stringArr [a];
lengthOfString = split_2.Length;
valX = int.Parse (split_2.Substring (0, split_2.IndexOf (',')));
valZ = int.Parse (split_2.Substring (split_2.IndexOf (' '), (lengthOfString - split_2.IndexOf (' '))));
for (int x1 = indexX; x1 < stringArr.Length - 1; x1++) {
split_3 = stringArr [x1];
lengthOfString = split_3.Length;
valX1 = int.Parse (split_3.Substring (0, split_3.IndexOf (',')));
valZ1 = int.Parse (split_3.Substring (split_3.IndexOf (' '), (lengthOfString - split_3.IndexOf (' '))));
//Check for the simillar x in the text file we provide
if (valX == valX1) {
xSortX [indexX] = valX1;
xSortZ [indexX] = valZ1;
//Debug.Log("X is " + valX + " and the coordinates which have simillar x ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1);
//Debug.Log("xSortX["+indexX+"] is :"+xSortX[indexX]);
//Debug.Log("xSortZ["+indexX+"] is :"+xSortZ[indexX]);
indexX++;
} else {
break;
}
}
loopRunX++;
//indexX = indexX + countx;
//Debug.Log("Next Index to check onwards : " + indexX);
//Debug.Log("Looping Count : " + loopRunX);
//Debug.Log("");
}
coordinatesX = new Vector3[count];
/*
* Adding the x and z coorinates values to Vector3 array to build the object in coordinates x
*/
for (int x =0; x<count; x++) {
Vector3 createVArray = new Vector3 (xSortX [x], 0, xSortZ [x]);
coordinatesX [x] = createVArray;
//Debug.Log("Coordinates X :"+coordinatesX[x]);
}
/*
* extracting simalar x coordinates
*/
//Debug.Log("");
//Debug.Log("-----------------extracting simalar Y coordinates----------------");
//Debug.Log("");
int[] checker = new int[count];
bool eq = false;
int indexY = 0;
loopRunX = 0;
for (int a = 0; a < stringArr.Length - 1; a++) {
split_2 = stringArr [a];
lengthOfString = split_2.Length;
valX = int.Parse (split_2.Substring (0, split_2.IndexOf (',')));
valZ = int.Parse (split_2.Substring (split_2.IndexOf (' '), (lengthOfString - split_2.IndexOf (' '))));
foreach (int check in checker) {
if (check == valZ) {
eq = false;
break;
} else {
eq = true;
}
}
if (eq) {
checker [a] = valZ;
for (int x1 = a; x1 < stringArr.Length - 1; x1++) {
split_3 = stringArr [x1];
lengthOfString = split_3.Length;
valX1 = int.Parse (split_3.Substring (0, split_3.IndexOf (',')));
valZ1 = int.Parse (split_3.Substring (split_3.IndexOf (' '), (lengthOfString - split_3.IndexOf (' '))));
//Check for the simillar x in the text file we provide
if (valZ == valZ1) {
ySortX [indexY] = valX1;
ySortZ [indexY] = valZ1;
//Debug.Log("Y is " + valZ + " and the coordinates which have simillar z ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1);
//Debug.Log("ySortX["+indexY+"] is :"+ySortX[indexY]);
//Debug.Log("ySortZ["+indexY+"] is :"+ySortZ[indexY]);
indexY++;
}
}
loopRunX++;
}
//Debug.Log("");
}
coordinatesY = new Vector3[count];
/*
* Adding the x and z coorinates values to Vector3 array to build the object in coordinates x
*/
for (int x =0; x<count; x++) {
Vector3 createVArray = new Vector3 (ySortX [x], 0, ySortZ [x]);
coordinatesY [x] = createVArray;
//Debug.Log("Coordinates Y :"+coordinatesY[x]);
}
}
I need to add this texture to my gameObject prefab
I am having issues with the output of the the result of a math calculation. I have a basic average of an array of double and I assign the result to a Label object, using the ToString() method. When I emulate the average, the label shows the correct value of 15.96 for example, but the same average of the same array, on my Galaxy S3 shows 159.6.
Is there anyone who know what's up and what can I do to make the S3 show the correct value?
Thank you all!
EDIT: passing the result to a label and adding the label to the grid:
double result = Math.Round(NP122.DoAverage(parameters), 2);
CustomLabel label = new CustomLabel();
label.ColNo = grid.ColumnDefinitions.IndexOf(c);
label.FontSize = 25;
label.TextColor = Color.Green;
if (result.ToString() == "NaN")
label.Text = "0";
else
label.Text = result.ToString();
label.IsVisible = true;
for (int i = 0; i < numberOfRows.Length + 2; i++) {
if(i == numberOfRows.Length +1)
Grid.SetRow(label, i);
}
Grid.SetColumn(label, grid.ColumnDefinitions.IndexOf(c));
listaRez.Add(label);
foreach (CustomLabel a in listaRez)
{
if (a.ColNo == grid.ColumnDefinitions.IndexOf(c))
{
grid.Children.Add(a);
}
}
EDIT 2: Custom function for NP122.DoAverage:
public static class NP122
{
public static double Vx, sx, Xm, kn, Xkinf, Xksup;
public static double sum;
public static double sumaProvizorie;
public static double[] valoriKn = new double[25];
public static double ValoareCaracteristicaSuperioara(double[] l)
{
Vx = 0;
sx = 0;
Xm = 0;
kn = 0;
Xkinf = 0;
Xksup = 0;
sum = 0;
sumaProvizorie = 0;
valoriKn[0] = 0;
//more here
valoriKn[24] = 0.35;
if (l.Length < 2 )
{
Xksup = 0;
Xkinf = 0;
}
else
{
Xm = (l.Sum()) / (l.Length);
for (int j = 0; j < l.Length; j++)
{
sumaProvizorie = Math.Round(Math.Pow((l[j] - Xm), 2), 2);
sum += sumaProvizorie;
}
kn = valoriKn[l.Length - 1];
double elements = (1.00 / (l.Length - 1));
double putere = sum;
sx = Math.Round(Math.Sqrt(elements * putere), 4);
Vx = sx / Xm;
Xksup = Xm * (1 + kn * Vx);
Xkinf = Xm * (1 - kn * Vx);
}
return Xksup;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a dungeon generator for a project I've been working on based off of this algorithm. I've gotten everything down, but my array (Fig. 1) doesn't seem to be holding giving the map data for some reason. I'm using three types of data to determine if a cell in the map is either empty (0), a space a character can be on (1), a hallway (2), or a wall (3).
I've gotten a bit stuck on this portion so any help is appreciated!
EDIT: The problem is the map object isn't storing the data in the loop shown in Fig. 1. Sorry for being so vague.
(Fig. 1)
for (int i = 0; i < roomList.Count; i++)
{
for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++)
{
for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++)
{
map[x, y] = 1;
}
}
}
(All of my relevant code)
namespace Project
{
}
public class Room
{
int xValue, yValue, widthValue, heightValue;
public int X
{
get { return xValue; }
set { xValue = value; }
}
public int Y
{
get { return yValue; }
set { yValue = value; }
}
public int W
{
get { return widthValue; }
set { widthValue = value; }
}
public int H
{
get { return heightValue; }
set { heightValue = value; }
}
}
public class DungeonGenerate
{
public int baseWidth = 513;
public int baseHeight = 513;
public int width = 64;
public int height = 64;
Color[,] arrayColor;
Random rand = new Random();
Room room = new Room();
Rectangle[,] rectMap;
public void Generate()
{
rectMap = new Rectangle[baseWidth, baseHeight];
//Creates a 2-D Array/Grid for the Dungeon
int[,] map = new int[baseWidth, baseHeight];
//Determines all the cells to be empty until otherwise stated
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
map[x, y] = 0;
}
}
//Determines the amount of rooms in the dungeon
int minRooms = (width * height) / 300;
int maxRooms = (width * height) / 150;
int amountOfRooms = rand.Next(minRooms, maxRooms);
//Room dimensions
int widthRoot = Convert.ToInt32(Math.Round(Math.Sqrt(width * 2)));
int heightRoot = Convert.ToInt32(Math.Round(Math.Sqrt(height * 2)));
int minWidth = Convert.ToInt32(Math.Round((width * .5) / widthRoot));
int maxWidth = Convert.ToInt32((width * 2) / widthRoot);
int minHeight = Convert.ToInt32(Math.Round(height * .5) / heightRoot);
int maxHeight = Convert.ToInt32((height * 2) / heightRoot);
//Creates the rooms
List<Room> roomList = new List<Room>(amountOfRooms);
for (int i = 0; i < amountOfRooms; i++)
{
bool ok = false;
do
{
room.X = rand.Next(width);
room.Y = rand.Next(height);
room.W = (rand.Next(maxWidth)) + minWidth;
room.H = (rand.Next(maxHeight)) + minHeight;
if (room.X + room.W >= width && room.Y + room.H >= height)
{
continue;
}
for (int q = 0; q < roomList.Count; q++)
{
if (room.X > roomList[q].X && room.X < roomList[q].X + room.W && room.Y > roomList[q].Y && room.Y < roomList[q].Y + room.H)
{
ok = false;
break;
}
}
ok = true;
roomList.Add(room);
} while (!ok);
}
//This will create hallways that lead to and from the rooms
int connectionCount = roomList.Count;
List<Point> connectedCells = new List<Point>((width * height));
for (int i = 0; i < connectionCount; i++)
{
Room roomA = roomList[i];
int roomNum = i;
while (roomNum == i)
{
roomNum = rand.Next(roomList.Count);
}
Room roomB = roomList[roomNum];
//Increasing this will make the hallway more straight, decreasing it will make the hallway more skewed
int sidestepChance = 10;
Point pointA = new Point(x: (rand.Next(roomA.W)) + roomA.X, y: (rand.Next(roomA.H)) + roomA.Y);
Point pointB = new Point(x: (rand.Next(roomB.W)) + roomB.X, y: (rand.Next(roomB.H)) + roomB.Y);
while (pointA != pointB)
{
int num = rand.Next() * 100;
if (num < sidestepChance)
{
if (pointB.X != pointA.X)
{
if (pointB.X > pointA.X)
{
pointB.X--;
}
else
{
pointB.X++;
}
}
}
else if(pointB.Y != pointA.Y)
{
if (pointB.Y > pointA.Y)
{
pointB.Y--;
}
else
{
pointB.Y++;
}
}
}
if (pointB.X < width && pointB.Y < height)
{
connectedCells.Add(pointB);
}
}
//Fills the room with data
for (int i = 0; i < roomList.Count; i++)
{
for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++)
{
for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++)
{
map[x, y] = 1;
}
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (map[x, y] == 0)
{
bool wall = false;
for (int yy = y - 2; yy < y + 2; yy++)
{
for (int xx = x - 2; xx < x + 2; xx++)
{
if (xx > 0 && yy > 0 && xx < width && yy < height)
{
if (map[xx, yy] == 1 || map[xx, yy] == 2)
{
map[x, y] = 3;
wall = true;
}
}
}
if (wall)
{
break;
}
}
}
}
}
//Rendering the Map and giving it some Color (Sort of)!
int scaler = baseWidth / width;
for (int x = 0; x < baseWidth; x++)
{
for (int y = 0; y < baseHeight; y++)
{
rectMap[x, y] = new Rectangle(x, y, 1, 1);
arrayColor = new Color[baseWidth, baseHeight];
switch (map[x, y])
{
case 0:
arrayColor[x, y] = new Color(0,0,0);
break;
case 1:
arrayColor[x, y] = new Color(0,0,0);
break;
case 2:
arrayColor[x, y] = new Color(0,0,0);
break;
case 3:
arrayColor[x, y] = new Color (0,0,0);
break;
}
}
}
}
public Rectangle[,] GetMap()
{
return rectMap;
}
public Color[,] GetColors()
{
return arrayColor;
}
}
In the for-loop where you're populating roomList, you're not instantiating a new Room each time. You're simply manipulating the same Room object and re-adding it to the list, so roomList will just contain many references to the same Room object. Try removing the room field from your DungeonGenerate class and use a local variable instead:
for (int i = 0; i < amountOfRooms; i++)
{
bool ok = false;
do
{
var room = new Room();
...
roomList.Add(room);
} while (!ok);
}