Skip to content

Instantly share code, notes, and snippets.

View Rayer's full-sized avatar

Rayer Rayer

View GitHub Profile
package com.rayer.util.string;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@Rayer
Rayer / gist:7ba704f442fa10bc326d
Last active August 29, 2015 14:02
Get argument using function frame
#include <iostream>
#include <cstdio>
using namespace std;
int testCall(int a, int b) {
void* p = __builtin_frame_address(0);
printf("Function pointer frame : %p\n", p);
printf("Param a pointer = %p\n", &a);
printf("Param b pointer = %p\n", &b);
#include <stdio.h>
#include <stdlib.h>
//CoR in C
//這組code可以再多做些什麼呢?
//1. 可以替每個handler加上priority
//2. 可以改成可變長度list,讓它可以在runtime加
//3. 甚至可以在每次處理以前動態產生一組handler list,可以動態調整
//4. (C++ only)可以做一個模板來compile time產生handler
@Rayer
Rayer / Iris.java
Created December 28, 2014 21:28
LibGDX's problem, sprite and MouseMove uses different coordinate system, and requires transformation before use.
@Override
public boolean mouseMoved(int screenX, int screenY) {
screenY -= Gdx.graphics.getHeight();
screenY = -screenY;
double length = Math.sqrt(Math.pow(((double)(screenX - sprite.getX())), 2d) + Math.pow((double)(screenY - sprite.getY()), 2d));
sprite.translate((float)((screenX - sprite.getX()) / length), (float)((screenY - sprite.getY()) / length));
System.out.println("Screen : " + screenX + "/" + screenY);
//Update camera position
//Camera.x - viewPort.width must > 0, or just set to 0
//Camera.y - viewPort.height must > 0, or just set to 0
//restrict 0, 0 side
float cameraX = actor.getX() - (camera.viewportWidth / 2) > 0 ? actor.getX() : camera.viewportWidth / 2;
float cameraY = actor.getY() - (camera.viewportHeight / 2) > 0 ? actor.getY() : camera.viewportHeight / 2;
//restrict h, w side
cameraX = actor.getX() + (camera.viewportWidth / 2) > mapSizeX ? mapSizeX - (camera.viewportWidth / 2) : cameraX;
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class foo {
std::string name;
public:
import java.util.Random;
class Untitled {
public static void main(String[] args) {
Random random = new Random();
for(int i = 0; i < 100; ++i) {
if(random.nextInt() % 4 == 0)
System.out.print("O");
else
@Rayer
Rayer / ObjectUtils.java
Created February 13, 2015 07:40
New or initialize a object with a parent class instance
public class ObjectUtils {
public static <T> T initByParent(T target, Object parent) throws IllegalAccessException {
Class<?> parentClass = target.getClass().getSuperclass();
if(parent.getClass() != parentClass) {
throw new IllegalArgumentException("Invalid parent!");
}
Field[] fields = parentClass.getDeclaredFields();
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int *i = 0;
i += 10;
cout << i << endl;
cout << *i << endl;
}
@Rayer
Rayer / ifWithBlock
Last active March 11, 2019 14:34
Using block in if statements
if (^(NSArray* searchImageSuffix) {
for(NSString* suffix in searchImageSuffix) {
if([shortURL hasSuffix:suffix])
return TRUE;
}
return FALSE;
}(searchImageSuffix))
{
addShortenedURLMenuItem(@"Image search by GOOGLE", [@"https://www.google.com/searchbyimage?&image_url=" stringByAppendingString: shortURL]);
}