public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the sides of Triangle: "); double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.nextDouble(); Triangle tri1 = new Triangle(a, b, c); double res = tri1.area(); if (a + b > c && b + c > a && c + a > b) { System.out.println("The area of triangle is: " + res); } else { System.out.println("This is not Triangle!"); } System.out.println("Enter the sides of another Triangle: "); double e = sc.nextDouble(); double f = sc.nextDouble(); double g = sc.nextDouble(); Triangle tri2 = new Triangle(e, f, g); res = tri2.area(); if (a + b > c && b + c > a && c + a > b) { System.out.println("The area of another triangle is: " + res); } else { System.out.println("This is not Triangle!"); } sc.close(); }
@Test public void test1() throws Exception { Triangle triangle = new Triangle(); int total = triangle.minimumTotal(ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2, 3))); assertEquals(3, total); }
/* * (non-Javadoc) * * @see de.jreality.soft.PolygonRasterizer#renderPolygon(de.jreality.soft.Polygon, * double[], boolean) */ public void renderTriangle(Triangle t, boolean outline) { transparency = t.getTransparency(); oneMinusTransparency = 1 - transparency; for (int i = 0; i < 3; i++) { double[] pi = triangle[i]; double[] vertexData = t.getPoint(i); double w = 1 / vertexData[Polygon.SW]; double wxy = w * mh; pi[Polygon.SX] = (wh + vertexData[Polygon.SX] * wxy); pi[Polygon.SY] = (hh - vertexData[Polygon.SY] * wxy); pi[Polygon.SZ] = (vertexData[Polygon.SZ] * w); pi[Polygon.R] = ((vertexData[Polygon.R] > 1 ? 255 : (255 * vertexData[Polygon.R]))); pi[Polygon.G] = ((vertexData[Polygon.G] > 1 ? 255 : (255 * vertexData[Polygon.G]))); pi[Polygon.B] = ((vertexData[Polygon.B] > 1 ? 255 : (255 * vertexData[Polygon.B]))); } writePolygon(triangle); count++; // if(p.getShader().isOutline()) // linePolygon(polygon); }
/** * Locate the triangle with point inside it or on its boundary. * * @param point the point to locate * @return the triangle that holds point; null if no such triangle */ public Triangle locate(Pnt point) { Triangle triangle = mostRecent; if (!this.contains(triangle)) triangle = null; // Try a directed walk (this works fine in 2D, but can // fail in 3D) Set<Triangle> visited = new HashSet<Triangle>(); while (triangle != null) { if (visited.contains(triangle)) { // This should // never // happen ; // System.out.println("Warning: Caught in a locate loop"); break; } visited.add(triangle); // Corner opposite point Pnt corner = point.isOutside(triangle.toArray(new Pnt[0])); if (corner == null) return triangle; triangle = this.neighborOpposite(corner, triangle); } // No luck; try brute force ; // System.out.println("Warning: Checking all triangles for " + point); for (Triangle tri : this) { if (point.isOutside(tri.toArray(new Pnt[0])) == null) return tri; } // No such triangle ; // System.out.println("Warning: No triangle holds " + point); return null; }
public static void main(String[] args) { Triangle tri = new Triangle(4.0, 5.0, 6.0); Equilateral equil = new Equilateral(5.0); IsocelesRight isoright = new IsocelesRight(1.5); System.out.println( "Triangle has sides A = " + tri.getSideA() + ", B = " + tri.getSideB() + ", C = " + tri.getSideC()); System.out.println( "Equilateral Triangle has sides A = " + equil.getSideA() + ", B = " + equil.getSideB() + ", C = " + equil.getSideC()); System.out.println( "Isoceles Right Triangle has sides A = " + isoright.getSideA() + ", B = " + isoright.getSideB() + ", C = " + isoright.getSideC()); }
/** * Intersect the given Ray with this shape. Tests for every triangle of the shape. Speed depends * hence on the complexity of the shape. * * @param Ray ray The Ray to be intersected * @param Matrix4f transformation The transformation of the Shape into its actual position * @return a RayShapeIntersection with the coordinates of the HitPoint if any. */ public RayShapeIntersection intersect(Ray ray, Matrix4f transformation) { RayShapeIntersection intersection = new RayShapeIntersection(); List<RayShapeIntersection> hitTriangles = new ArrayList<RayShapeIntersection>(); // get all triangles Iterator<Triangle> it = getTriangles().iterator(); Triangle triangle; // calculate intersection of ray and triangle while (it.hasNext()) { triangle = it.next(); triangle.transform(transformation); intersection = calculateIntersection(ray, triangle); if (intersection.hit) { hitTriangles.add(intersection); } } Vector3f distance = new Vector3f(); float shortestDist = Float.MAX_VALUE; for (RayShapeIntersection in : hitTriangles) { distance.sub(ray.getOrigin(), in.hitPoint); if (distance.length() < shortestDist) { intersection = in; shortestDist = distance.length(); } } // return the intersected point if any return intersection; }
@Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); // Logger.d(TAG, "onTouchEvent: " + event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isTouched = true; this.setLayoutParams(newLp); endPoint.x = (int) downX; endPoint.y = (int) downY; changeViewHeight( this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); postInvalidate(); downX = event.getX() + location[0]; downY = event.getY() + location[1]; // Logger.d(TAG, String.format("downX: %f, downY: %f", downX, downY)); break; case MotionEvent.ACTION_MOVE: // 计算直角边和斜边(用于计算绘制两圆之间的填充去) triangle.deltaX = event.getX() - downX; triangle.deltaY = -1 * (event.getY() - downY); // y轴方向相反,所有需要取反 double distance = Math.sqrt(triangle.deltaX * triangle.deltaX + triangle.deltaY * triangle.deltaY); triangle.hypotenuse = distance; // Logger.d(TAG, "triangle: " + triangle); refreshCurRadiusByMoveDistance((int) distance); endPoint.x = (int) event.getX(); endPoint.y = (int) event.getY(); postInvalidate(); break; case MotionEvent.ACTION_UP: isTouched = false; this.setLayoutParams(originLp); if (isArrivedMaxMoved) { // 触发事件 changeViewHeight(this, originWidth, originHeight); postInvalidate(); if (null != onDraggableFlagViewListener) { onDraggableFlagViewListener.onFlagDismiss(this); } Logger.d(TAG, "触发事件..."); resetAfterDismiss(); } else { // 还原 changeViewHeight(this, originWidth, originHeight); startRollBackAnimation(500 /*ms*/); } downX = Float.MAX_VALUE; downY = Float.MAX_VALUE; break; } return true; }
public static void main(String[] args) { // square Square square = new Square(5.6); System.out.println("area: " + square.getArea()); System.out.println("perimeter: " + square.getPerimeter()); // rectangle Rectangle rectangle = new Rectangle(1.2, 3.4); System.out.println("area: " + rectangle.getArea()); System.out.println("perimeter: " + rectangle.getPerimeter()); // circle Circle circle = new Circle(1.2); System.out.println("area: " + circle.getArea()); System.out.println("perimeter: " + circle.getPerimeter()); // triangle Triangle triangle = new Triangle(1.2, 1.2, 1.2); System.out.println("area: " + triangle.getArea()); System.out.println("perimeter: " + triangle.getPerimeter()); // shape Shape shape = new Circle(1); System.out.println("area: " + shape.getArea()); System.out.println("perimeter: " + shape.getPerimeter()); }
protected void calculateTriangles() { if ((!trianglesDirty) && (triangle != null)) { return; } if (points.length >= 6) { float area = 0; for (int i = 0; i < (points.length / 2) - 1; i++) { float x1 = points[(i * 2)]; float y1 = points[(i * 2) + 1]; float x2 = points[(i * 2) + 2]; float y2 = points[(i * 2) + 3]; area += (x1 * y2) - (y1 * x2); } area /= 2; triangle = new TriangleNeat(); for (int i = 0; i < points.length; i += 2) { triangle.addPolyPoint(points[i], points[i + 1]); } triangle.triangulate(); } trianglesDirty = false; }
/** * Draw the triangles list to the applet * * @param g Graphics to draw on * @param drawSettings override the draw color, texture (and other settings). Draw whole object in * the given color if != null */ public void drawTriangles(PGraphics g, DrawSettings drawSettings) { if (!texturesInitialized) setTextureImage(); synchronized (triangles) { for (Triangle tri : triangles) { tri.draw(g, drawSettings); } } }
/** * Report neighbor opposite the given vertex of triangle. * * @param site a vertex of triangle * @param triangle we want the neighbor of this triangle * @return the neighbor opposite site in triangle; null if none * @throws IllegalArgumentException if site is not in this triangle */ public Triangle neighborOpposite(Pnt site, Triangle triangle) { if (!triangle.contains(site)) throw new IllegalArgumentException("Bad vertex; not in triangle"); for (Triangle neighbor : triGraph.neighbors(triangle)) { if (!neighbor.contains(site)) return neighbor; } return null; }
public static void main(String[] args) { // Figure f = new Figure(10, 10); Rectangle r = new Rectangle(9, 5); Triangle t = new Triangle(10, 8); System.out.println("Pole: " + r.area()); System.out.println("Pole: " + t.area()); }
public static void main(String args[]) { ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>(); ArrayList tmp = new ArrayList(); tmp.add(-10); triangle.add(tmp); Triangle sol = new Triangle(); System.out.print(sol.minimumTotal(triangle)); }
@Test public void test() { Triangle t = new Triangle("20,50", "100,90", "70,150"); assertTrue(t.isRightAngle()); t.setTriangle("30,75", "25,0", "-50,45"); assertFalse(t.isRightAngle()); }
@Test public void test4() throws Exception { Triangle triangle = new Triangle(); int total = triangle.minimumTotal( ImmutableList.of( ImmutableList.of(-1), ImmutableList.of(3, 2), ImmutableList.of(-3, 1, -1))); assertEquals(-1, total); }
/** * Maps one Coordinate to another. * * @param c a Coordinate which must be inside one of the triangle keys passed into the constructor * @return the transformed Coordinate */ public Coordinate transform(Coordinate c) { monitor.report(++coordinatesTransformed, -1, "coordinates"); Triangle sourceTriangle = sourceTriangle(c); Assert.isTrue(sourceTriangle != null, "Unable to determine source triangle for " + c); Triangle destTriangle = destTriangle(sourceTriangle); return destTriangle.toEuclideanCoordinate(sourceTriangle.toSimplicialCoordinate(c)); }
public static void main(String args[]) { Scanner s = new Scanner(System.in); Point x = new Point(); Triangle t = new Triangle(); LineSegment l = new LineSegment(); /*System.out.println("Enter Tester Point Values:"); x.setFromUser(); System.out.println("Enter Tester Triangle Values:"); t.setFromUser(); System.out.println("Enter Tester Line Values:"); l.setFromUser(); */ boolean exit = false; while (!exit) { System.out.println("Check for point or line is in a triangle or want to exit (1/2/3)"); int choice = s.nextInt(); if (choice == 1) { x.setFromUser(); System.out.println("Check if is inside line or triangle or exit(1/2/3)"); int secondchoice = s.nextInt(); if (secondchoice == 1) { l.setFromUser(); System.out.println(l.isInside(x)); } else if (secondchoice == 2) { t.setFromUser(); System.out.println(t.isInside(x)); } else if (secondchoice == 3) { exit = true; } } else if (choice == 2) { l.setFromUser(); System.out.println("Check if is inside triangle or exit(1/2)"); int secondchoice = s.nextInt(); if (secondchoice == 1) { t.setFromUser(); System.out.println(t.isInside(x)); } else if (secondchoice == 2) { exit = true; } } else if (choice == 3) { exit = true; } } }
/** prints the 3 vertex Coordinates(x,y,z) to the array */ void write(float v[]) { if (level == 0) { a.write(v); b.write(v); c.write(v); } else { child0.write(v); child1.write(v); child2.write(v); } }
private Triangle sourceTriangle(Coordinate c) { for (Iterator i = triangleMap.keySet().iterator(); i.hasNext(); ) { Triangle triangle = (Triangle) i.next(); if (triangle.getEnvelope().contains(c) && triangle.contains(c)) { return triangle; } } return null; }
@Override public Vec normalAt(Point3D intersection, Ray ray) { for (Triangle tri : mesh) { if (tri.intersectTri(intersection, ray)) { Vec v1 = new Vec(tri.getP1(), tri.getP0()); Vec v2 = new Vec(tri.getP2(), tri.getP0()); return Vec.crossProd(v1, v2); } } return null; }
public static void main(String[] args) { // TODO Auto-generated method stub Point p1 = new Point(0, 0); Point p2 = new Point(-1, -2); Point p3 = new Point(20, 31); Triangle tri = new Triangle(p1, p2, p3); System.out.println(tri.toString()); Shape[] a = {new Circle(5), new Triangle(p1, p2, p3)}; }
/** * Searches all triangles which intersect the given ray (rayStart, rayEnd) and adds them to * intersectedTriangles. Not only the segment between rayStart and rayEnd is checked but the whole * ray from -infinity to +infinity. * * @param rayStart start point of the ray. * @param rayEnd end point of the ray. * @param intersectedTriangles list where to add intersecting triangles */ public void getIntersectedTriangles( final Point3f rayStart, final Point3f rayEnd, final ArrayList<IntersectedTriangle> intersectedTriangles) { for (Triangle tri : triangles) { Point3f intersect = new Point3f(); if (tri.intersectsRay(rayStart, rayEnd, intersect)) intersectedTriangles.add(new IntersectedTriangle(tri, intersect)); } }
@Override // first, I create object of Triangle, the identical one to our object Triangle2 with the left // angle point in (0,0) // after it, I move it on with move method of Triangle public void move(double dx, double dy) { Triangle triangle = new Triangle( new Point(0, 0), new Point(0, base), new Point(height / Math.tan((angle)), height)); triangle.move(dx, dy); System.out.println("Our triangle's left base angle was in (0,0). Now it's moved"); }
public static void main(String[] args) { ApplicationContext beanFactory = new ClassPathXmlApplicationContext("XML/beanFactoryPostProcessor.xml"); Triangle triangle = (Triangle) beanFactory.getBean("triangle"); System.out.println("Point A"); System.out.println("X : " + triangle.getPointA().getX()); System.out.println("Y : " + triangle.getPointA().getX()); System.out.println("Point B "); System.out.println("X : " + triangle.getPointB().getX()); System.out.println("Y : " + triangle.getPointB().getY()); }
public static void main(String[] args) { Square square = new Square(4); Triangle triangle = new Triangle(3, 2, 3, 7.2, 5, 4.5); Rectangle rectangle = new Rectangle(6, 4); Circle circle = new Circle(8.5); System.out.println(square.getArea()); System.out.println(triangle.getArea()); System.out.println(triangle.getWidth()); System.out.println(triangle.getHeight()); System.out.println(rectangle.getArea()); System.out.println(circle.getArea()); }
private List<Triangle> getTriangles() { List<Triangle> triangles = new ArrayList<Triangle>(); IntBuffer verticesBuffer = mVertexBuffers.getVertexBuffer(); int[] verticesInt = new int[verticesBuffer.capacity()]; for (int i = 0; i < verticesBuffer.capacity(); i++) { verticesInt[i] = verticesBuffer.get(i); } // Fixed Point Conversion float[] vertices = new float[verticesInt.length]; for (int i = 0; i < verticesInt.length; i++) { vertices[i] = (float) verticesInt[i] / 65536; } ShortBuffer indicesBuffer = mVertexBuffers.getIndexBuffer(); short[] indices = new short[indicesBuffer.capacity()]; for (int i = 0; i < indicesBuffer.capacity(); i++) { indices[i] = indicesBuffer.get(i); } Triangle triangle; Point3f vec; for (int i = 0; i < indices.length; i++) { triangle = new Triangle(); vec = new Point3f(); vec.x = vertices[indices[i] * 3]; vec.y = vertices[indices[i] * 3 + 1]; vec.z = vertices[indices[i] * 3 + 2]; triangle.mX = vec; i++; vec = new Point3f(); vec.x = vertices[indices[i] * 3]; vec.y = vertices[indices[i] * 3 + 1]; vec.z = vertices[indices[i] * 3 + 2]; triangle.mY = vec; i++; vec = new Point3f(); vec.x = vertices[indices[i] * 3]; vec.y = vertices[indices[i] * 3 + 1]; vec.z = vertices[indices[i] * 3 + 2]; triangle.mZ = vec; triangles.add(triangle); } return triangles; }
public void setByTriangle(Triangle t) { a = t.getX().x - t.getO().x; b = t.getY().x - t.getO().x; c = t.getO().x; d = t.getX().y - t.getO().y; e = t.getY().y - t.getO().y; f = t.getO().y; }
public static void main(String[] args) { Circle c = new Circle(); Triangle t = new Triangle(); Rectangle r = new Rectangle(); // 1. 각각의 도형 그리기 c.draw(); t.draw(); r.draw(); // 2. 각각의 도형 지우기 c.delete(); t.delete(); r.delete(); }
public Pnt[] getContourForSite(Pnt sitex) { HashSet<Pnt> done = new HashSet<Pnt>(initialTriangle); for (Triangle triangle : dt) for (Pnt site : triangle) { if (done.contains(site)) continue; done.add(site); List<Triangle> list = dt.surroundingTriangles(site, triangle); Pnt[] vertices = new Pnt[list.size()]; int i = 0; for (Triangle tri : list) vertices[i++] = tri.getCircumcenter(); // draw(vertices, withFill ? getColor(site) : // null); if (site == sitex) return vertices; } return null; }
public void onDrawFrame(GL10 gl) { // Redraw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Starting to set camera position"); float[] mViewMatrix = new float[16]; // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); float[] mMVPMatrix = new float[16]; // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Drawing ClientSide"); float[] scratch = new float[16]; // Create a rotation transformation for the triangle long time = SystemClock.uptimeMillis() % 4000L; float angle = 0.090f * ((int) time); Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f); // Combine the rotation matrix with the projection and camera view // Note that the mMVPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0); // Draw triangle mTriangle.draw_ClientSide(scratch); }