/** * Draws a triangle from the given vertex data. * * @param aTriangleBuffer The buffer containing the vertex data. */ public void addObject(float x, float y, float z, Shape shape) { PrimProperties a = new PrimProperties(); // new prim properties for size and center Prim result = null; switch (shape) { case BOX: a.setCenter(new Vector3d(x, y, z)); a.setSize(new Vector3d(.5f, .5f, .5f)); result = new Box(a); // new box object result.createVertices(); break; case SPHERE: a.setCenter(new Vector3d(x, y, z)); a.setSize(new Vector3d(1, 5, 5)); result = new Sphere(a); result.createVertices(); // new sphere object goes here break; case TETRAHEDRON: a.setCenter(new Vector3d(x, y, z)); a.setSize(new Vector3d(1, 1, 1)); // new tetrahedron object goes here break; case LINE: a.setCenter(new Vector3d(x, y, z)); a.setSize(new Vector3d(1, 1, 1)); } if (result != null) { synchronized (this.objects) { objects.add(result); } } }
public void addStringsToStringtable() { StringTable stable = serializer.getStringTable(); super.addStringsToStringtable(); for (Relation i : contents) { for (Member j : i.getMembers()) { stable.incr(j.getRole()); } } }
/* - Initialize data structures for use. - Times the execution of the greedy algorithm. - Passes the results to the logger. */ public static void main(String[] args) { logger = new Logger(args); n = (int) Integer.parseInt(args[0]); seed = (int) Integer.parseInt(args[1]); vertices = new LinkedHashMap<Integer, Integer>(n); g = new Graph(n); long startTime = System.currentTimeMillis(); generateVertices(); generateAdjacencyMatrix(); Prim.prim(g); g.preorder(); long endTime = System.currentTimeMillis(); logger.logCoordinates(vertices); logger.logAdjacencyMatrix(g.getMatrix(), g.mst(), false); logger.logAdjacencyMatrix(g.getMatrix(), g.mst(), true); logger.logMSTWeight(g.cost()); logger.logEdgeTour(g.traversal()); logger.logOptimalPath(g.distance(), g.traversal()); logger.logRuntime(endTime - startTime); }
@Override public void onDrawFrame(GL10 glUnused) { GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); // Set our per-vertex lighting program. GLES20.glUseProgram(mPerVertexProgramHandle); // Set program handles for cube drawing. mMVPMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVPMatrix"); mMVMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVMatrix"); mLightPosHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_LightPos"); mPositionHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Position"); mColorHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Color"); mNormalHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Normal"); mTextureCoordinateHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_TexCoordinate"); // Set the active texture unit to texture unit 0. GLES20.glEnable(GLES20.GL_TEXTURE_2D); // Bind the texture to this unit. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureArray.get(0)); drawPlane(this.mGroundVertices, this.mGroundColors, this.mGroundNormals, mGroundTextures); // bind the next texture GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureArray.get(1)); drawPlane(this.mOceanVertices, this.mOceanColors, this.mOceanNormals, mOceanTextures); // bind the next texture GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureArray.get(3)); drawCube(); // Get changes in finger positions and add to total for moving and rotating totalDiffX += diffX; totalDiffY += diffY; angleX += dx; angleY += dy; // to prevent the user from looking more than straight up and less and straight down. if (Math.abs(angleY) > 1.5f) { angleY -= dy; } // move the model matrix by finger distance changes for pinch zoom movement. Matrix.translateM(mModelMatrix, 0, this.diffX, this.diffY, 0); // rotate camera based on finger movements this.mCamera.look.x = (float) Math.cos(angleX); this.mCamera.look.y = (float) Math.sin(angleX); this.mCamera.look.z = (float) Math.tan(angleY); updateLookAt(); // reset variables so that movement stops after finger movement stops this.dx = 0; this.dy = 0; this.diffX = 0; this.diffY = 0; // update the lookAt coordinates updateLookAt(); // redraw each object in the array. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureArray.get(2)); synchronized (this.objects) { for (Prim p : this.objects) { p.draw( this.mPositionHandle, this.mColorHandle, this.mNormalHandle, this.mTextureCoordinateHandle); } } GLES20.glDisable(GLES20.GL_TEXTURE_2D); }
@Override public boolean onTouch(View v, MotionEvent event) { // x and y coordinates of touch location on screen. float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Initial pointer down mode = LOOK; Vector3d near = unProject(x, y, 0, mMVPMatrix, this.width, this.height); Vector3d far = unProject(x, y, 1, mMVPMatrix, this.width, this.height); Vector3d pickingRay = far.subtract(near); Vector3d normal = new Vector3d(0, 0, 1); if (normal.dot(pickingRay) != 0 && pickingRay.z < 0) { Vector3d diff = new Vector3d(-totalDiffX, -totalDiffY, 0); float t = (-2f - normal.dot(diff)) / (normal.dot(pickingRay)); pickingRay = diff.add(pickingRay.scale(t)); boolean b = false; synchronized (this.objects) { int counter = 0; // counter for checking current position. List<Integer> positions = new ArrayList< Integer>(); // array for storing object array positions that you've "touched" for (Prim p : this.objects) { Vector3d objectCenter = p.getCenter(); float d = (((objectCenter.subtract(near)).cross(objectCenter.subtract(far))).length()) / ((far.subtract(near)).length()); if (d < p.getSize().x || d < p.getSize().y || d < p.getSize().z) { positions.add( counter); // add position value to array for checking if closest to camera than // others you touched. b = true; // stops from creating new objects so you don't erase and create at the // same time. } counter++; } if (positions.size() > 0) { Vector3d currentCenter; // variable where current center will be stored. currentCenter = objects .get(positions.get(0)) .getCenter(); // get the first center for distance checking double minDistance = Math.sqrt( (double) (Math.pow((double) currentCenter.x - totalDiffX, 2.0) + Math.pow((double) currentCenter.y - totalDiffY, 2.0) + Math.pow( (double) currentCenter.z, 2.0))); // distance formula of the first position. the first // position is initially set as the smallest. int finalMin = 0; // int value where the smallest distance position is stored for removal for (int i = 1; i < positions.size(); i++) // starting at one because 0 was already checked before the loop { currentCenter = objects .get(positions.get(i)) .getCenter(); // get the current center of the object for testing distance. double currentDistance = Math.sqrt( (double) (Math.pow((double) currentCenter.x - totalDiffX, 2.0) + Math.pow((double) currentCenter.y - totalDiffY, 2.0) + Math.pow( (double) currentCenter.z, 2.0))); // distance formula for the current position. This is // tested to see if smaller than the one before it. if (currentDistance < minDistance) { minDistance = currentDistance; // min becomes the current because current is smaller finalMin = i; // current position is stored as smallest distance position. } } this.objects.remove( (int) positions.get(finalMin)); // remove object closest to camera that you touch. } counter = 0; } if (b == false && pickingRay.x < 254.75 && pickingRay.x > .25 && pickingRay.y > .25 && pickingRay.y < 254.75) { addObject(pickingRay.x, pickingRay.y, pickingRay.z + .25f, Shape.BOX); } } break; case MotionEvent.ACTION_POINTER_DOWN: // non-primary pointer down oldDist = spacing(event); // check distance as one finger may be reported as multiple ones very close together if (oldDist > 10f) { mode = ZOOM; } break; case MotionEvent.ACTION_MOVE: if (event.getPointerCount() >= 2 && this.mode != ZOOM) { oldDist = spacing(event); // check distance as one finger may be reported as multiple ones very close together if (oldDist > 10f) { mode = ZOOM; } } if (mode == ZOOM) { float newDist = spacing(event); float diff = newDist - oldDist; if (newDist > 10f && Math.abs(diff) > 20f) { Vector3d lookAtVector = this.mCamera.look.subtract(this.mCamera.eye); lookAtVector = lookAtVector.normalize(); lookAtVector = lookAtVector.scale(diff * .00025f); this.diffX -= lookAtVector.x; this.diffY -= lookAtVector.y; } } else if (mode == LOOK) { float dx = x - mPreviousX; float dy = y - mPreviousY; this.dx = dx * .008f; this.dy = dy * .008f; updateLookAt(); } break; case MotionEvent.ACTION_POINTER_UP: if (event.getPointerCount() == 1) { mode = LOOK; } case MotionEvent.ACTION_UP: mode = NONE; break; } mPreviousX = x; mPreviousY = y; return true; }