List of usage examples for com.google.common.math IntMath divide
@SuppressWarnings("fallthrough") public static int divide(int p, int q, RoundingMode mode)
From source file:org.terasology.engine.subsystem.awt.cities.Sectors.java
/** * @param wx the world block x coord//w ww . j a v a 2 s.c o m * @param wz the world block z coord * @return the sector */ public static Sector getSectorForBlock(int wx, int wz) { int sx = IntMath.divide(wx, Sector.SIZE, RoundingMode.FLOOR); int sz = IntMath.divide(wz, Sector.SIZE, RoundingMode.FLOOR); return getSector(new Point2i(sx, sz)); }
From source file:org.terasology.commonworld.Sectors.java
/** * @param wx the world block x coord/*from ww w .j a v a 2 s . c om*/ * @param wz the world block z coord * @return the sector */ public static Sector getSectorForBlock(int wx, int wz) { int sx = IntMath.divide(wx, Sector.SIZE, RoundingMode.FLOOR); int sz = IntMath.divide(wz, Sector.SIZE, RoundingMode.FLOOR); return getSector(new Vector2i(sx, sz)); }
From source file:com.github.benmanes.caffeine.cache.simulator.parser.umass.storage.StorageTraceReader.java
@Override public LongStream events() throws IOException { return lines().flatMapToLong(line -> { String[] array = line.split(",", 5); if (array.length <= 4) { return LongStream.empty(); }/*w w w .j a v a2s .c o m*/ long startBlock = Long.parseLong(array[1]); int size = Integer.parseInt(array[2]); int sequence = IntMath.divide(size, BLOCK_SIZE, RoundingMode.UP); char readWrite = Character.toLowerCase(array[3].charAt(0)); return (readWrite == 'w') ? LongStream.empty() : LongStream.range(startBlock, startBlock + sequence); }); }
From source file:org.terasology.cities.generator.RoundHouseGenerator.java
/** * @param lot the lot to use/*from w w w. jav a 2 s . c o m*/ * @return a generated {@link RoundHouse} model */ public RoundHouse generate(SimpleLot lot) { // make build-able area 1 block smaller, so make the roof stay inside Rectangle lotRc = Rectangles.expandRect(lot.getShape(), -1); int centerX = lotRc.x + IntMath.divide(lotRc.width, 2, RoundingMode.HALF_UP); int centerY = lotRc.y + IntMath.divide(lotRc.height, 2, RoundingMode.HALF_UP); int towerSize = Math.min(lotRc.width, lotRc.height); int towerRad = towerSize / 2; int entranceWidth = 1; int entranceHeight = 2; Rectangle doorRc = new Rectangle(centerX + towerRad, centerY, 1, entranceWidth); Orientation doorOrientation = Orientation.EAST; Vector2i doorDir = doorOrientation.getDir(); Rectangle probeRc = new Rectangle(doorRc.x + doorDir.x, doorRc.y + doorDir.y, doorRc.width, doorRc.height); int baseHeight = heightMap.apply(probeRc.x, probeRc.y); int sideHeight = 4; RoundHouse house = new RoundHouse(new Vector2i(centerX, centerY), towerRad, baseHeight, sideHeight); SimpleDoor entrance = new SimpleDoor(doorOrientation, doorRc, baseHeight, baseHeight + entranceHeight); house.setDoor(entrance); int windowWidth = 1; Rectangle wndRc1 = new Rectangle(centerX - towerRad, centerY, 1, windowWidth); Rectangle wndRc2 = new Rectangle(centerX, centerY - towerRad, 1, windowWidth); Rectangle wndRc3 = new Rectangle(centerX, centerY + towerRad, 1, windowWidth); SimpleWindow wnd1 = new SimpleWindow(Orientation.WEST, wndRc1, baseHeight + 1, baseHeight + 2); SimpleWindow wnd2 = new SimpleWindow(Orientation.NORTH, wndRc2, baseHeight + 1, baseHeight + 2); SimpleWindow wnd3 = new SimpleWindow(Orientation.SOUTH, wndRc3, baseHeight + 1, baseHeight + 2); house.addWindow(wnd1); house.addWindow(wnd2); house.addWindow(wnd3); return house; }
From source file:main.OverlayExamplePanel.java
@Override protected void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; Font derivedFont = g.getFont().deriveFont(Font.PLAIN, fontSize); Random r = new Random(123456); int gridX = 20; int gridY = 20; int width = IntMath.divide(getWidth(), gridX, RoundingMode.CEILING); int height = IntMath.divide(getHeight(), gridY, RoundingMode.CEILING); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float cx = r.nextFloat(); float cy = r.nextFloat(); Color color = colormap.getColor(cx, cy); img.setRGB(x, y, color.getRGB()); }//from w ww .java 2s . c o m } // width * gridX is not identical with getWidt() due to rounding g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(img, 0, 0, width * gridX, height * gridY, null); FontRenderContext frc = g.getFontRenderContext(); String text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt " + "ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo " + "dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. "; AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, derivedFont); AttributedCharacterIterator iterator = attributedString.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc); g.setColor(overlayColor); float tx = 0; float ty = 0; while (measurer.getPosition() < iterator.getEndIndex()) { TextLayout layout = measurer.nextLayout(getWidth()); ty += layout.getAscent(); float dx = 0; layout.draw(g, tx + dx, ty); ty += layout.getDescent() + layout.getLeading(); } }
From source file:edu.mit.streamjit.impl.compiler2.FullDataParallelAllocationStrategy.java
@Override public void allocateGroup(ActorGroup group, Range<Integer> iterations, List<Core> cores, Configuration config) { int coresSize = Math.min(cores.size(), maxNumCores); int perCore = IntMath.divide(iterations.upperEndpoint() - iterations.lowerEndpoint(), coresSize, RoundingMode.CEILING); for (int i = 0; i < coresSize && !iterations.isEmpty(); ++i) { int min = iterations.lowerEndpoint(); Range<Integer> allocation = group.isStateful() ? iterations : iterations.intersection(Range.closedOpen(min, min + perCore)); cores.get(i).allocate(group, allocation); iterations = Range.closedOpen(allocation.upperEndpoint(), iterations.upperEndpoint()); }//from w w w. j ava 2s . c o m assert iterations.isEmpty(); }
From source file:de.fhg.igd.iva.explorer.main.OverlayExamplePanel.java
@Override protected void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; Font derivedFont = g.getFont().deriveFont(Font.PLAIN, fontSize); Random r = new Random(123456); int gridX = 20; int gridY = 20; int width = IntMath.divide(getWidth(), gridX, RoundingMode.CEILING); int height = IntMath.divide(getHeight(), gridY, RoundingMode.CEILING); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double cx = r.nextDouble(); double cy = r.nextDouble(); Color color = colormap.getColor(cx, cy); img.setRGB(x, y, color.getRGB()); }//from ww w . j ava 2 s . c o m } // width * gridX is not identical with getWidt() due to rounding g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(img, 0, 0, width * gridX, height * gridY, null); FontRenderContext frc = g.getFontRenderContext(); String text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt " + "ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo " + "dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. "; AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, derivedFont); AttributedCharacterIterator iterator = attributedString.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc); g.setColor(overlayColor); float tx = 2; // add some tiny distance offset to the top/left corner float ty = 2; while (measurer.getPosition() < iterator.getEndIndex()) { TextLayout layout = measurer.nextLayout(getWidth()); ty += layout.getAscent(); float dx = 0; layout.draw(g, tx + dx, ty); ty += layout.getDescent() + layout.getLeading(); } }
From source file:org.ldp4j.http.AlternativeProvider.java
AlternativeProvider(List<MediaType> mediaTypes, List<CharacterEncoding> characterEncodings, List<Language> languages) { this.mediaTypes = EntityProvider.of(mediaTypes); this.characterEncodings = EntityProvider.of(characterEncodings); this.languages = EntityProvider.of(languages); this.size = this.mediaTypes.consumableEntities() * this.characterEncodings.consumableEntities() * this.languages.consumableEntities(); this.position = 0; this.buckets = IntMath.divide(this.size, MAX_BUCKETS, RoundingMode.CEILING); }
From source file:org.fenixedu.cms.ui.AdminPosts.java
@RequestMapping(value = "{slug}", method = RequestMethod.GET) public String posts(Model model, @PathVariable(value = "slug") String slug, @RequestParam(required = false, defaultValue = "1") int page) { Site site = Site.fromSlug(slug);// w w w. j a v a 2 s . c o m AdminSites.canEdit(site); model.addAttribute("site", site); Set<Post> posts = site.getPostSet(); int pages = IntMath.divide(posts.size(), PER_PAGE, RoundingMode.CEILING); if (page < 1) { page = 1; } if (pages > 0 && page > pages) { page = pages; } model.addAttribute("currentPage", page); model.addAttribute("pages", pages); model.addAttribute("posts", posts.stream().sorted(Post.CREATION_DATE_COMPARATOR).skip((page - 1) * PER_PAGE) .limit(PER_PAGE).collect(Collectors.toList())); return "fenixedu-cms/posts"; }
From source file:org.terasology.StaticCities.bldg.gen.DefaultBuildingGenerator.java
private Building generateRoundHouse(StaticParcel staticParcel, InfiniteSurfaceHeightFacet heightFacet) { // make build-able area 1 block smaller, so make the roof stay inside Rect2i lotRc = staticParcel.getShape().expand(new Vector2i(-1, -1)); int centerX = lotRc.minX() + IntMath.divide(lotRc.width(), 2, RoundingMode.HALF_DOWN); // width() is 1 too much int centerY = lotRc.minY() + IntMath.divide(lotRc.height(), 2, RoundingMode.HALF_DOWN); int towerSize = Math.min(lotRc.width(), lotRc.height()); int towerRad = towerSize / 2 - 1; int entranceHeight = 2; ImmutableVector2i doorPos = new ImmutableVector2i(centerX + towerRad, centerY); Orientation orient = Orientation.EAST; ImmutableVector2i doorDir = orient.getDir(); Vector2i probePos = new Vector2i(doorPos.x() + doorDir.getX(), doorPos.y() + doorDir.getY()); int baseHeight = TeraMath.floorToInt(heightFacet.getWorld(probePos)) + 1; int sideHeight = 4; SimpleRoundHouse house = new SimpleRoundHouse(orient, new ImmutableVector2i(centerX, centerY), towerRad, baseHeight, sideHeight);//w w w. j av a 2 s . c o m SimpleDoor entrance = new SimpleDoor(orient, doorPos, baseHeight, baseHeight + entranceHeight); house.getRoom().addDoor(entrance); int wndOff = 1; ImmutableVector2i wndPos1 = new ImmutableVector2i(centerX - towerRad, centerY); ImmutableVector2i wndPos2 = new ImmutableVector2i(centerX, centerY - towerRad); ImmutableVector2i wndPos3 = new ImmutableVector2i(centerX, centerY + towerRad); SimpleWindow wnd1 = new SimpleWindow(Orientation.WEST, wndPos1, baseHeight + wndOff); SimpleWindow wnd2 = new SimpleWindow(Orientation.NORTH, wndPos2, baseHeight + wndOff); SimpleWindow wnd3 = new SimpleWindow(Orientation.SOUTH, wndPos3, baseHeight + wndOff); house.getRoom().addWindow(wnd1); house.getRoom().addWindow(wnd2); house.getRoom().addWindow(wnd3); return house; }