/** * Gets the distance between two dots at a given set * * @param dot1 the first dot * @param dot2 the second dot * @param set the set to calculate the distance between the two dots * @return the distance between the dots at the input set */ public static double distance(Dot dot1, Dot dot2, int set) { // TODO: add exception checking for IndexOutOfBoundsException double ftb1 = dot1.getFrontToBack(set); double ftb2 = dot2.getFrontToBack(set); double sts1 = dot1.getSideToSide(set); double sts2 = dot2.getSideToSide(set); double ftb = ftb1 - ftb2; double sts = sts1 - sts2; return Math.sqrt(ftb * ftb + sts * sts); }
@Override protected void onDraw(Canvas canvas) { if (dots == null) return; canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setStyle(Style.FILL); for (Dot dot : dots.getDots()) { paint.setColor(dot.getColor()); canvas.drawCircle(dot.getX(), dot.getY(), dot.getDiameter(), paint); } }
public void setAdapter(int paramInt) { this.mDots.clear(); int i = 0; int k; for (int j = 0; i < paramInt; j = k) { Dot localDot = new Dot(); k = j + 1; localDot.id = j; this.mDots.add(localDot); i++; } initDotsCoordinates(); }
private void move() { if (isAtEdge(cat)) { lose(); return; } Vector<Dot> avaliable = new Vector<>(); Vector<Dot> positive = new Vector<>(); HashMap<Dot, Integer> al = new HashMap<Dot, Integer>(); for (int i = 1; i < 7; i++) { Dot n = getNeighbour(cat, i); if (n.getStatus() == Dot.STATUS_OFF) { avaliable.add(n); al.put(n, i); if (getDistance(n, i) > 0) { positive.add(n); } } } if (avaliable.size() == 0) { win(); } else if (avaliable.size() == 1) { MoveTo(avaliable.get(0)); } else { Dot best = null; if (positive.size() != 0) { // 存在可以直接到达屏幕边缘的走 System.out.println("向前"); int min = 999; for (int i = 0; i < positive.size(); i++) { int a = getDistance(positive.get(i), al.get(positive.get(i))); if (a < min) { min = a; best = positive.get(i); } } MoveTo(best); } else { // 有方向都存在路障 System.out.println("躲路"); int max = 0; for (int i = 0; i < avaliable.size(); i++) { int k = getDistance(avaliable.get(i), al.get(avaliable.get(i))); if (k <= max) { max = k; best = avaliable.get(i); } } MoveTo(best); } } }
public synchronized void setSelection(int position) { if ((position < 0) || (position >= mDots.size())) { throw new IllegalArgumentException("Position is out of bounds:" + position); } for (Dot dot : mDots) { if (dot.id == position) { dot.isSelected = true; } else { dot.isSelected = false; } } isSelected = true; invalidate(); }
private void handleClick(Dot selected) { if ((prevSelected == null) || (prevSelected.equals(selected) == false)) { if (mItemClickListener != null) { mItemClickListener.onItemClick(null, this, selected.id, selected.id); } prevSelected = selected; } }
@Override protected synchronized void onDraw(Canvas canvas) { if ((mThumb != null) && (mDots.size() > 1)) { if (isSelected) { for (Dot dot : mDots) { if (dot.isSelected) { Rect bounds = mThumb.copyBounds(); bounds.right = dot.mX; bounds.left = dot.mX; mThumb.setBounds(bounds); break; } } } else { int intervalWidth = mDots.get(1).mX - mDots.get(0).mX; Rect bounds = mThumb.copyBounds(); // find nearest dot if ((mDots.get(mDots.size() - 1).mX - bounds.centerX()) < 0) { bounds.right = mDots.get(mDots.size() - 1).mX; bounds.left = mDots.get(mDots.size() - 1).mX; mThumb.setBounds(bounds); for (Dot dot : mDots) { dot.isSelected = false; } mDots.get(mDots.size() - 1).isSelected = true; handleClick(mDots.get(mDots.size() - 1)); } else { for (int i = 0; i < mDots.size(); i++) { if (Math.abs(mDots.get(i).mX - bounds.centerX()) <= (intervalWidth / 2)) { bounds.right = mDots.get(i).mX; bounds.left = mDots.get(i).mX; mThumb.setBounds(bounds); mDots.get(i).isSelected = true; handleClick(mDots.get(i)); } else { mDots.get(i).isSelected = false; } } } } } super.onDraw(canvas); }
private int getDistance(Dot one, int dir) { // System.out.println("X:"+one.getX()+" Y:"+one.getY()+" Dir:"+dir); int distance = 0; if (isAtEdge(one)) { return 1; } Dot ori = one, next; while (true) { next = getNeighbour(ori, dir); if (next.getStatus() == Dot.STATUS_ON) { return distance * -1; } if (isAtEdge(next)) { distance++; return distance; } distance++; ori = next; } }
private void redraw() { Canvas c = getHolder().lockCanvas(); c.drawColor(Color.LTGRAY); Paint paint = new Paint(); paint.setFlags(Paint.ANTI_ALIAS_FLAG); for (int i = 0; i < ROW; i++) { int offset = 0; if (i % 2 != 0) { offset = WIDTH / 2; } for (int j = 0; j < COL; j++) { Dot one = getDot(j, i); switch (one.getStatus()) { case Dot.STATUS_OFF: paint.setColor(0xFFEEEEEE); break; case Dot.STATUS_ON: paint.setColor(0xFFFFAA00); break; case Dot.STATUS_IN: paint.setColor(0xFFFF0000); break; default: break; } c.drawOval( new RectF( one.getX() * WIDTH + offset, one.getY() * WIDTH, (one.getX() + 1) * WIDTH + offset, (one.getY() + 1) * WIDTH), paint); } } getHolder().unlockCanvasAndPost(c); }
/** * Returns the distance between a pair of coordinates and a dot at a set * * @param frontToBack the front-to-back coordinate * @param sideToSide the side-to-side coordinate * @param dot the dot * @param set the set number * @return the distance between the coordinates and the dot at the given set */ public static double distance(double frontToBack, double sideToSide, Dot dot, int set) { // TODO: add exception checking for IndexOutOfBoundsException double ftb = frontToBack - dot.getFrontToBack(set); double sts = sideToSide - dot.getSideToSide(set); return Math.sqrt(ftb * ftb + sts * sts); }
private void parseRect() { // Список символов List<List<Dot>> dotvv = new ArrayList<List<Dot>>(); for (int i = 0; i < tc.getSymbols().size(); i++) { List<Dot> dotv = new ArrayList<Dot>(); // Список определений символов // Нарисовать элементы Symbol // Нарисовать строки из полей Field for (int j = 0; j < tc.getSymbols().get(i).getField2s().size(); j++) { if (tc.getSymbols().get(i).getField2s().get(j).getShow() == 1) { Dot dd1 = new Dot( (tc.getSymbols().get(i).getPos().getX() + tc.getSymbols().get(i).getField2s().get(j).getPos().getX()), (tc.getSymbols().get(i).getPos().getY() + tc.getSymbols().get(i).getField2s().get(j).getPos().getY())); dotv.add(dd1); if (ModelDSN.debug >= 2) System.out.println("0.Added x-" + dd1.getX() + ", y-" + dd1.getY()); } } for (int j = 0; j < tc.getSymbolDefs().size(); j++) { if (tc.getSymbols().get(i).getId() == tc.getSymbolDefs().get(j).getId()) { // Координаты правого нижнего угла for (int k = 0; k < tc.getSymbolDefs().get(j).getTinyCadSymbol().getRectangles().size(); k++) { Dot dd1 = new Dot( tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getRectangles() .get(k) .getA() .getX(), tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getRectangles() .get(k) .getA() .getY()); Dot dd2 = new Dot( tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getRectangles() .get(k) .getB() .getX(), tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getRectangles() .get(k) .getB() .getY()); dotv.add(dd1); dotv.add(dd2); } // Наристовать строки for (int k = 0; k < tc.getSymbolDefs().get(j).getTinyCadSymbol().getTexts().size(); k++) { Dot dd1 = new Dot( tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getTexts() .get(k) .getDp() .getX(), tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getTexts() .get(k) .getDp() .getY()); Dot dd2 = new Dot( tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getTexts() .get(k) .getDp() .getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getTexts() .get(k) .getStr() .length(), tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getTexts() .get(k) .getDp() .getY() + 3); if (ModelDSN.debug >= 2) System.out.println("1.Added x-" + dd1.getX() + ", y-" + dd1.getY()); if (ModelDSN.debug >= 2) System.out.println("2.Added x-" + dd2.getX() + ", y-" + dd2.getY()); if (ModelDSN.debug >= 1) System.out.println( "Str - " + tc.getSymbolDefs().get(j).getTinyCadSymbol().getTexts().get(k).getStr()); dotv.add(dd1); dotv.add(dd2); } // Нарисовать круги for (int k = 0; k < tc.getSymbolDefs().get(j).getTinyCadSymbol().getEllipses().size(); k++) { int x1 = Math.round( (tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getEllipses() .get(k) .getA() .getX())); int y1 = Math.round( (tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getEllipses() .get(k) .getA() .getY())); int x2 = Math.round( (tc.getSymbols().get(i).getPos().getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getEllipses() .get(k) .getB() .getX())); int y2 = Math.round( (tc.getSymbols().get(i).getPos().getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getEllipses() .get(k) .getB() .getY())); Dot dd1 = new Dot(Math.min(x1, x2), Math.min(y1, y2)); Dot dd2 = new Dot( Math.min(x1, x2) + (Math.max(x1, x2) - Math.min(x1, x2)), Math.min(y1, y2) + (Math.max(y1, y2) - Math.min(y1, y2))); dotv.add(dd1); dotv.add(dd2); } // Нарисовать POLYGON линии for (int k = 0; k < tc.getSymbolDefs().get(j).getTinyCadSymbol().getPolygons().size(); k++) { Pos dp = tc.getSymbolDefs().get(j).getTinyCadSymbol().getPolygons().get(k).getDp(); for (int l = 0; l < tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getPolygons() .get(k) .getDpointv() .size(); l++) { Dot dd1 = new Dot( tc.getSymbols().get(i).getPos().getX() + dp.getX(), tc.getSymbols().get(i).getPos().getY() + dp.getY()); Dot dd2 = new Dot( tc.getSymbols().get(i).getPos().getX() + dp.getX() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getPolygons() .get(k) .getDpointv() .get(l) .getDp() .getX(), tc.getSymbols().get(i).getPos().getY() + dp.getY() + tc.getSymbolDefs() .get(j) .getTinyCadSymbol() .getPolygons() .get(k) .getDpointv() .get(l) .getDp() .getY()); dotv.add(dd1); dotv.add(dd2); } } } } dotvv.add(dotv); } // Отсортировать список dotvv по координатам x for (int i = 0; i < dotvv.size(); i++) { Collections.sort(dotvv.get(i), new DotXComparator()); tc.getModules() .add( new Module( new Dot(dotvv.get(i).get(0).getX(), 0), new Dot(dotvv.get(i).get(dotvv.get(i).size() - 1).getX(), 0))); } // Отсортировать список dotvv по координатам y for (int i = 0; i < dotvv.size(); i++) { Collections.sort(dotvv.get(i), new DotYComparator()); String val = "No name"; List<Field2> allFields = new ArrayList<Field2>(); for (int j = 0; j < tc.getSymbols().get(i).getField2s().size(); j++) { if (tc.getSymbols().get(i).getField2s().get(j).getDescription().equals("Ref")) { val = tc.getSymbols().get(i).getField2s().get(j).getValue(); } allFields.add(tc.getSymbols().get(i).getField2s().get(j)); } tc.getModules() .set( i, new Module( new Dot(tc.getModules().get(i).getA().getX(), dotvv.get(i).get(0).getY()), new Dot( tc.getModules().get(i).getB().getX(), dotvv.get(i).get(dotvv.get(i).size() - 1).getY()), val, allFields)); if (ModelDSN.debug >= 2) { System.out.println( "X1-" + tc.getModules().get(i).getA().getX() + ", Y1-" + tc.getModules().get(i).getA().getX() + ", X2-" + tc.getModules().get(i).getB().getX() + ", Y2-" + tc.getModules().get(i).getB().getY()); } } }
/** dot coordinates. */ private void initDotsCoordinates() { float intervalWidth = (getWidth() - (mThumb.getRadius() * 2)) / (mDots.size() - 1); for (Dot dot : mDots) { dot.mX = (int) (mThumb.getRadius() + intervalWidth * (dot.id)); } }
private Dot getNeighbour(Dot one, int dir) { switch (dir) { case 1: return getDot(one.getX() - 1, one.getY()); case 2: if (one.getY() % 2 == 0) { return getDot(one.getX() - 1, one.getY() - 1); } else { return getDot(one.getX(), one.getY() - 1); } case 3: if (one.getY() % 2 == 0) { return getDot(one.getX(), one.getY() - 1); } else { return getDot(one.getX() + 1, one.getY() - 1); } case 4: return getDot(one.getX() + 1, one.getY()); case 5: if (one.getY() % 2 == 0) { return getDot(one.getX(), one.getY() + 1); } else { return getDot(one.getX() + 1, one.getY() + 1); } case 6: if (one.getY() % 2 == 0) { return getDot(one.getX() - 1, one.getY() + 1); } else { return getDot(one.getX(), one.getY() + 1); } default: break; } return null; }
private boolean isAtEdge(Dot d) { if (d.getX() * d.getY() == 0 || d.getX() + 1 == COL || d.getY() + 1 == ROW) { return true; } return false; }
private void MoveTo(Dot one) { one.setStatus(Dot.STATUS_IN); getDot(cat.getX(), cat.getY()).setStatus(Dot.STATUS_OFF); ; cat.setXY(one.getX(), one.getY()); }