Diamond drawer script_Java

Diamond Drawer

The programme allows the user to create a diamond shape on canvas based on the user’s input. The user will enter 3 parameters: x and y coordinate of the diamond tip, and diamond size.

The canvas size is 40×20. User are only allowed to input an x value between 1 and 40 and y value between 1 and 20. Diamond size can only be even number. If the diamond size is too big for the canvas, the program will ask the user to input the diamond size again.

Attention: string input will crash the program.

Pseudo Code

Define Terms:
0.1 let ROWS be the canvas row unber, and COLS be the canvas column number.
0.2 let input_num be the diamond size which holds the users’ input
0.3 let cor_x, cor_y be the coordinates of the diamond tip.
0.4 let diamond_chart be the 2-dimensional canvas chart.

Start:
1. Initiate the canvas by filling diamond_chart with space characters.
2. Draw the x and y axis using characters such as ‘+’, ‘|’, ‘-’, ‘^’, ‘>’
3. cor_x ← user input, if the user’s input an invalid number, it asks the user to input again.
4. cor_y ← user input, if the user’s input an invalid number, it asks the user to input again.
5. input_num ← user input, if the user input is too big for the canvas, it asks the user to input again.
6. Let diamond_row ← CORW + 1- cor_y (calculate the diamond tip position on canvas)
7. Let diamond_col ← cor_x +2 (calculate the diamond tip position on canvas)
8. Use for loop to draw the upper diamond
9. Use for loop to draw the lower diamond
10. Print the diamond and axis

//Program Name: DiamondDraw
//Author: Di Wang
//Date: 2016 9 29
//Compiler: JDK1.8
//IDE: Eclipse

import java.util.Scanner;
public class DiamondDraw {

public static void main(String[] args) {
// define the chart dimension
final int ROWS = 20;
final int COLS = 40;
int input_num;
int cor_x;
int cor_y;
// define a 2d array of empty characters.
char [][] diamond_chart= new char [ROWS+3][COLS+3];

// create an empty canvas by filling in space characters
for(int i=0; i< (ROWS+3); i++){
for(int j=0; j< (COLS+3); j++){
diamond_chart [i][j] = ' ';
}
}

//initiate a scanner object
Scanner input = new Scanner(System.in);

// draw the y axis
for (int i = 1; i=0 ; i -=10){
diamond_chart [i][1] = ‘0’;
diamond_chart [i][0] = Character.forDigit((ROWS+1-i)/10, 10);

if((i-5)>0){
diamond_chart [i-5][1] = ‘5’;
diamond_chart [i-5][0] = Character.forDigit((ROWS+1-i)/10, 10);
}
}

// draw the x axis
for (int i = 2; i’;

// draw numbers on the x axis
for(int i= 2; i<(COLS+3); i+=10){
diamond_chart [ROWS+2][i] = '0';
diamond_chart [ROWS+2][i-1] = Character.forDigit(i/10, 10);

if(i+5 COLS||cor_xROWS||cor_y < 1);

// user will only allowed create a limited size diamond
do{
System.out.println("Enter the diamond size (only odd number accepted) :");
System.out.println("Make sure the dimond size is small enough to fit into the canvas.");
input_num = input.nextInt();
}while(cor_x < (input_num/2) || cor_y < input_num || input_num%2==0);

// draw the diamond
int diamond_row = ROWS+1-cor_y;
int diamond_col = cor_x + 2 ;

for (int i = 0; i<= input_num; i +=1){
if (i<=input_num/2)
{
//upper diamond
for (int j = 0; j input_num/2)
{
//lower diamond
for (int j = input_num-i-1; j>=0; j–){
diamond_chart [diamond_row + i][diamond_col-j]=’*’;
diamond_chart [diamond_row + i][diamond_col+j]=’*’;
//System.out.println(j);
}

}
}

//print out the shape
for(int i = 0; i < (ROWS+3); i++){
for (int j= 0 ; j < (COLS+3); j++){
System.out.print(diamond_chart[i][j]);
}
System.out.println();
}

}

}

searching Prime number algorithm

1 st method (very inefficient):

get value for an number n
if n =2, output “It is a prime number”
set the value i =2;
set the value isPrime = True;
if i if n % i == 0, isPrime=False;
else i + 1;
output “It is a prime number” if isPrime is true
output “It is a not prime number” if isPrime is False.

The above method is inefficient. If the input is a very big prime number, it will make division from 2 all the way to n-1.

2nd method (slightly faster):

If :
an integer x < another integer n,
√n % x != 0 (n is not divisible by x )
then: n² % x !=0 (n² is not divisible by x)

Vise versa, n % x == 0, then n² % x ==0 ;

So, we can implement the 1st method, by looping only from 2 to n. we just have to modify the above algorithm “i<n” into “i<√n”. it optimized (n ) algorithm into (√n) algorithm.

3rd method (Sieve of Eratosthene):
This method works when given a number n and get all the prime numbers that is smaller than n. It filters out all the composite numbers, and left the prime number.

It works in this way:

  1. given a number n,
  2. in the range of 2 to n, find the prime number one by one.
    • the first prime number is 2, then filter out all the numbers that is multiple of 2.
    • find the next prime number, which is 3, and filter out all the multiples of 3.
    • find the next prime number 5, and filter out all the multiples of 5.
    • continue the above process. until the searched prime number reaches n.
  3. output all the remaining numbers.

 

The pseudo code would be:

  1. given a number n, n>1
  2. define a list IsPrime[], indexed from 2 to n, initially all items are set to True
  3. i start from 2, if i < n, repeat the following steps:

if IsPrime[i] == true:

set IsPrime[ i2, i2+i, i2+2i, i2+3i, …] to False (not exceeding n)

      4.output the indices in IsPrime[] ‘s item that is true.

I have scripted it in python. To see the code and test it in codeSkulptor, please click the link below.
http://www.codeskulptor.org/#user41_lovrNkO0EW_4.py

Reference:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
https://codility.com/programmers/lessons/9/
https://codility.com/media/train/8-PrimeNumbers.pdf
http://research.cs.wisc.edu/techreports/1990/TR909.pdf

———————————————–
Designed by Freepik

Using JavaScript for Minecraft


Minecraft Mod lets players to extend the functionality (or cheat) using Javascript.

you will need:

  • CanaryMod
  • Scriptcraft

download the latest versions  here.

CanaryMod is a Server that allows custom mods (such as Scriptcraft) to be added. So ScriptCraft must be used with CanaryMod.

Installation:

download CanaryMod.jar. In the command prompt type:

java -jar canarymod.jar      //  you should use the actual file name instead of “canarymod.jar ”

you will need to run it 3 time: as the first installation will ask you to modify elua.txt. you Must change the setting “eula=true”. Second time you running it, CanaryMod will run properly.

place scriptcraft.jar into the plugin folder and run the Server again, or just type reload in the command prompt.

Permission:

In the CanaryMod command window type js 1 + 1 and hit enter. You should see 2 . but in order for you to use Javascript command in game, you should add you as the operator.

go to  ops.cfg in the config folder, use notepad to open it. add your UUID at the bottom, then Done! ( I have tried to use my minecraft ID, it didn’t work. )

————–

http://scriptcraftjs.org/

https://github.com/walterhiggins/ScriptCraft

 Unity: Real-Time Rendering Fail!

Unity Logo

I am trying to use game engine to do real-time rendering for Visualization. Game Engine such as Unreal and Unity uses pre-baked lighting and shader. Theoretically, it can render up to 60 frames/ second.  while v-ray for maya may take up 10 minutes to render single image.

V-ray is very  powerful in terms of creating render passes for composting . But Game Engine prevails at time-saving.

Unreal Engine provides very powerful tools for real-time rendering. But in Unity, I have to attached to a script manually to render, and it is very buggy.

The script is like following, attach it to the camera to use:

———————————-

using UnityEngine;
using System.Collections;

public class CameraCapture : MonoBehaviour {

// Set the render directory and frame-rate.

public string folder = “C:/Users/******”;
public int frameRate = 25;

void Start()
{
// Set the playback framerate (real time will not relate to game time after this).
Time.captureFramerate = frameRate;

// Create the folder
System.IO.Directory.CreateDirectory(folder);
}

// Update is called once per frame
void LateUpdate ()
{
// Append filename to folder name (format is ‘0005 shot.png”‘)
string name = string.Format(“{0}/{1:D04} shot.png”, folder, Time.frameCount);

// Capture the screenshot to the specified file.
Application.CaptureScreenshot(name);
}
}

—————————————

IT DOESN”T WORK !!!!!  You get this:

0003 shot

The only way to render it out is to turn off anti-alias setting (menu: Edit > Project Settings > Quality): Disabled

0002 shot

We are loosing the quality for not getting smoothing contours from the geometry.  Bummer!

 

 

Python Game:

I had made a few game using python in the past few years. It was a practice for python syntax and algorithm.

icon

I used CodeSkuptor. It allows python programs to run on your browser. Click the upper left “play” button to run the games. ( CodeSkulptor runs in Chrome 18+, Firefox 11+, and Safari 6+)

 

Space Shooter (Rice Rock):

space shotter

Arrow: move

Space: shoot

http://www.codeskulptor.org/#user38_1PZUwhP1IV_1.py

 

Black Jack:

black jack

 

2048:

 

Automate Maya Rendering Process

Maya Batch Render doesn’t allow user to render multiple shots with one click. What if I want to let the computer create a render queue and automate the rendering process? You may install backburner to solve this. or there is another simple way of doing so without setting up the Manager and Server.

First of all, create a .bat file from Windows desktop.

The Template is like this:

C:\Progra~1\Autodesk\Maya2014\bin\render -r file -s <starting_frame> -e <ending_frame> -cam <camera> -rd “<render_location>” “<maya_file_directory>”

here under are the possible settings that you can modify

-r file: render from scene setting.

-s : starting frame

-e: ending frame

-cam: camera Name

-rd: where you want to save rendered image / where is the maya file.

for more info for rendering command, refer to:  http://www.quilime.com/code/maya_render_bat

So You can create a list for rendering queue, like:

maya render

Suspend and Resume Batch Render:

suspend.png

  1. install Advanced Process Explorer
  2. Run it.
  3. Find mayabatch.exe, right click, suspend.

From Animation to Programming

In the past 10 years I worked mostly in the CG industry as an instructor, animator and special effect artist. It was an exciting and rewarding experience. But I have made my decision to move on to programming . Not only is because I reached a bottleneck in my career, but also I had seen how Computer Science had changed the industry  completely.

The Market is a sinkingship, People learn to survive. So, New Journey starts here….

Python HTML/CSS and Javascript.