/// <summary> /// Draws the contents of the answers array into the table images of a picture box. /// </summary> /// <param name="tablePictureBox">Picture box contaning the table image.</param> /// <param name="answers">A three-dimensional array of yes/no answers.</param> /// <param name="tableIndex">The index of the table in the answers array.</param> /// <param name="drawSomething">A delegate which will draw into the cells.</param> /// <param name="penColor">The color of the pen to be used for drawing.</param> public static void DrawAnswers(PictureBox tablePictureBox, bool[,,] answers, int tableIndex, DrawSomething drawSomething, Color penColor) { if (answers == null) { return; } // the table images have one extra column and one extra row that doesn't contain answers but annotations int tableRows = answers.GetUpperBound(1) + 2; int tableColumns = answers.GetUpperBound(2) + 2; int cellHeight = tablePictureBox.Height / tableRows; int cellWidth = tablePictureBox.Width / tableColumns; using (var graphics = Graphics.FromImage(tablePictureBox.Image)) using (var pen = new Pen(penColor, 2)) { for (int row = 0; row < tableRows - 1; row++) { for (int col = 0; col < tableColumns - 1; col++) { if (answers[tableIndex, row, col] == true) { // there is one extra column and one extra row in the actual table image (with annotations) drawSomething(row + 1, col + 1, cellWidth, cellHeight, graphics, pen); } } } } tablePictureBox.Refresh(); }
/// <summary> /// Checks whether an answer is selected in each row. /// (there can't be two or more answers selected thanks to the implementation of HandlePictureBoxClicks() ) /// </summary> /// <param name="answers">A three-dimensional array containing the answers. (=Array of tables)</param> /// <param name="tableIndex">The index of the table to check.</param> /// <returns></returns> public static bool CheckAnswers(bool[,,] answers, int tableIndex) { int tableRows = answers.GetUpperBound(1) + 1; int tableColumns = answers.GetUpperBound(2) + 1; for (int row = 0; row < tableRows; row++) { bool rowContainsAnswer = false; for (int col = 0; col < tableColumns; col++) { if (answers[tableIndex, row, col] == true) { rowContainsAnswer = true; break; } } if (!rowContainsAnswer) { return(false); } } return(true); }
/// <summary> /// Computes a ratio of correctly identified answers (values) among all answers from an answer sheet. /// </summary> private float ComputeCorrectness(bool[,,] studentExpectedValues, bool[,,] studentComputedValues, bool[,,] answerExpectedValues, bool[,,] answerComputedValues) { int studentTableRows = studentExpectedValues.GetUpperBound(1) + 1; int studentTableColumns = studentExpectedValues.GetUpperBound(2) + 1; int answerTables = answerExpectedValues.GetUpperBound(0) + 1; int answerTableRows = answerExpectedValues.GetUpperBound(1) + 1; int answerTableColumns = answerExpectedValues.GetUpperBound(2) + 1; int correctValues = 0; int totalValues = studentTableRows + (answerTableRows * 3); // check the student table values for (int row = 0; row < studentTableRows; row++) { bool valueCorrect = true; for (int col = 0; col < studentTableColumns; col++) { if (studentExpectedValues[0, row, col] != studentComputedValues[0, row, col]) { valueCorrect = false; break; } } if (valueCorrect) { correctValues++; } } // check the answer table values for (int table = 0; table < answerTables; table++) { for (int row = 0; row < answerTableRows; row++) { bool valueCorrect = true; for (int col = 0; col < answerTableColumns; col++) { if (answerExpectedValues[table, row, col] != answerComputedValues[table, row, col]) { valueCorrect = false; break; } } if (valueCorrect) { correctValues++; } } } return((float)correctValues / (float)totalValues); }
static void DumpZPlane3D(bool[,,] trid, int plane) { for (var j = 0; trid.GetUpperBound(0) >= j; ++j) { for (var i = 0; trid.GetUpperBound(1) >= i; ++i) { Debug.Write(trid[plane, j, i] ? '#' : '.'); } Debug.Write("\n"); } }
/// <summary> /// This function converts answers from a table (answer table or student table) into DbSets that inherit from KlokanDBAnswer. /// Uses reflection and therefore is not suitable for automated batch processing. /// Special table values: /// No answer: '\0' (or -1 for number) /// Multiple answers: 'x' (or int.MaxValue for number) /// </summary> /// <typeparam name="T">The specific DbSet type that inherits from KlokanDBAnswer.</typeparam> /// <param name="answers">A three-dimensional table of answers.</param> /// <param name="tableIndex">The index of a table which is to be converted.</param> /// <param name="isStudentTable">A flag that tells whether the table is a student table and therefore should be treated slightly differently.</param> public static List <T> AnswersToDbSet <T>(bool[,,] answers, int tableIndex, bool isStudentTable) where T : KlokanDBAnswer { int tableRows = answers.GetUpperBound(1) + 1; int tableColumns = answers.GetUpperBound(2) + 1; List <T> chosenAnswersDB = new List <T>(); for (int row = 0; row < tableRows; row++) { int markedColumn = -1; // find out the marked column (marked column can stay -1 in case the question wasn't answered) for (int col = 0; col < tableColumns; col++) { int numberOfSelectedAnswers = 0; if (answers[tableIndex, row, col] == true) { markedColumn = col; numberOfSelectedAnswers++; } // keep in mind that more answers can be selected if (numberOfSelectedAnswers > 1) { markedColumn = int.MaxValue; } } // reflection is fine to use here because this function is never called in automated batch processing // it is always called in forms which allow the user to edit 1 sheet var answerInstance = Activator.CreateInstance(typeof(T)) as T; if (isStudentTable) { // student table answers are assigned to questions with negative numbers answerInstance.QuestionNumber = row - 5; answerInstance.Value = markedColumn.ToString(); } else { answerInstance.QuestionNumber = (row + 1) + (tableIndex * 8); char enteredValue = (char)('a' + markedColumn); answerInstance.Value = new String(enteredValue, 1); } chosenAnswersDB.Add(answerInstance); } return(chosenAnswersDB); }
/// <summary> /// Handles clicks for table images, stores chosen answers in a data structure /// and ensures only one answer is selected for each question. /// </summary> /// <param name="mouseEvent">The click event.</param> /// <param name="pictureBox">A reference to the picture box that was clicked.</param> /// <param name="tableIndex">The index of the table that was clicked.</param> /// <param name="answers">A three-dimensional array of answers (yes/no) that are displayed in the table image.</param> public static void HandleTableImageClicks(MouseEventArgs mouseEvent, PictureBox pictureBox, int tableIndex, bool[,,] answers) { // the tables have one extra column and one extra row that doesn't contain answers but annotations int tableRows = answers.GetUpperBound(1) + 2; int tableColumns = answers.GetUpperBound(2) + 2; int cellHeight = pictureBox.Height / tableRows; int cellWidth = pictureBox.Width / tableColumns; int rowClicked = mouseEvent.Y / cellHeight; int columnClicked = mouseEvent.X / cellWidth; // if the area designated for answers was clicked // (the third and fourth condition is true when the very edge of the table image is clicked) if (rowClicked != 0 && columnClicked != 0 && rowClicked < tableRows && columnClicked < tableColumns) { Image tableImage = pictureBox.Image; using (var graphics = Graphics.FromImage(tableImage)) using (var blackPen = new Pen(Color.Black, 2)) { // if this answer has already been selected, unselect it if (answers[tableIndex, rowClicked - 1, columnClicked - 1] == true) { answers[tableIndex, rowClicked - 1, columnClicked - 1] = false; RemoveCellContent(rowClicked, columnClicked, cellWidth, cellHeight, graphics); } // else unselect all other answers in this row and select this one instead else { // remove any crosses that are already in the row for (int i = 0; i < tableColumns - 1; i++) { if (answers[tableIndex, rowClicked - 1, i] == true) { answers[tableIndex, rowClicked - 1, i] = false; RemoveCellContent(rowClicked, i + 1, cellWidth, cellHeight, graphics); } } answers[tableIndex, rowClicked - 1, columnClicked - 1] = true; DrawCross(rowClicked, columnClicked, cellWidth, cellHeight, graphics, blackPen); } } pictureBox.Refresh(); } }
public void SetTerrain(bool[, ,] filled) { int width = (filled.GetUpperBound(0) + 1); int height = (filled.GetUpperBound(2) + 1); int maxY = filled.GetUpperBound(1) + 1; int chunksX = (width / 16) + 1; int chunksY = (height / 16) + 1; for (int x = 0; x < chunksX; x++) { for (int y = 0; y < chunksY; y++) { var terr = new VoxelTerrainChunk(); terr.Size = new Point3(16, 64, 16); //terr.Size = new Point3(5, 5, 5); terr.WorldPosition = Vector3.Modulate(terr.Size.ToVector3() * terr.NodeSize, new Vector3(x, 0, y)); terr.Create(); TW.Data.GetSingleton <Datastore>().Persist(terr); for (int tx = 0; tx < terr.Size.X; tx++) { for (int ty = 0; ty < terr.Size.Y; ty++) { for (int tz = 0; tz < terr.Size.Z; tz++) { var heightMapX = tx + (int)terr.WorldPosition.X; var heightMapZ = tz + (int)terr.WorldPosition.Z; if (heightMapX >= width || heightMapZ >= height) { continue; } if (ty >= maxY) { continue; } terr.GetVoxelInternal(new Point3(tx, ty, tz)).Filled = filled[heightMapX, ty, heightMapZ]; } } } } } }
static int CountNeighbors3D(bool[,,] trid, int z, int y, int x) { int found = 0; for (var k = Math.Max(0, z - 1); Math.Min(z + 1, trid.GetUpperBound(0)) >= k; ++k) { for (var j = Math.Max(0, y - 1); Math.Min(y + 1, trid.GetUpperBound(1)) >= j; ++j) { for (var i = Math.Max(0, x - 1); Math.Min(x + 1, trid.GetUpperBound(2)) >= i; ++i) { if (z == k && y == j && x == i) { continue; } if (trid[k, j, i]) { found++; } } } } return(found); }
/// <summary> /// Converts the answers in the student number table into an actual number. /// </summary> /// <returns>Returns -1 if the conversion failed.</returns> private int StudentTableToNumber(bool[,,] studentTableValues) { int tableRows = studentTableValues.GetUpperBound(1) + 1; int tableColumns = studentTableValues.GetUpperBound(2) + 1; int studentNumber = 0; for (int row = 0; row < tableRows; row++) { bool numberInRow = false; for (int col = 0; col < tableColumns; col++) { if (studentTableValues[0, row, col] == true) { // if a row contains two numbers (answers) if (numberInRow == true) { return(-1); } numberInRow = true; studentNumber *= 10; studentNumber += col; } } // if a row doesn't contain a number (an answer) if (numberInRow == false) { return(-1); } } return(studentNumber); }