Check Overlap
unknown
java
2 years ago
3.6 kB
12
Indexable
Never
package crossCheck; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class CheckOverlap { public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); FileInputStream fis = new FileInputStream(new File("/home/thaontv/Downloads/input_test.xlsx")); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sheet = wb.getSheetAt(0); FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); for (Row row : sheet) //iteration over row using for each loop { if (row == sheet.getRow(0)) continue; boolean validInput = true; for (int i = 0; i < 6; i++) { if (formulaEvaluator.evaluateInCell(row.getCell(i)).getCellType() != CellType.NUMERIC) { validInput = false; } ; } if (validInput) { int x1 = (int) row.getCell(0).getNumericCellValue(); int y1 = (int) row.getCell(1).getNumericCellValue(); int l1 = (int) row.getCell(2).getNumericCellValue(); int x2 = (int) row.getCell(3).getNumericCellValue(); int y2 = (int) row.getCell(4).getNumericCellValue(); int l2 = (int) row.getCell(5).getNumericCellValue(); // System.out.println("Check overlap: " + checkOverlap(x1, y1, l1, x2, y2, l2)); run.setText("Check overlap: " + checkOverlap(x1, y1, l1, x2, y2, l2)); } else { // System.out.print("Invalid input"); run.setText("Invalid input"); } run.addBreak(); } FileOutputStream out = new FileOutputStream(new File("/home/thaontv/Downloads/output_test.docx")); document.write(out); out.close(); System.out.println("written successully"); } static boolean checkOverlap(int x1, int y1, int l1, int x2, int y2, int l2) { boolean check1 = checkXLine(x1 - l1, x1 + l1, x2 - l2, x2 + l2, y1, y2); boolean check2 = checkYLine(y1 - l1, y1 + l1, y2 - l2, y2 + l2, x1, x2); boolean check3 = checkXYLine(x1 - l1, x1 + l1, y1, y2 - l2, y2 + l2, x2); boolean check4 = checkXYLine(x2 - l2, x2 + l2, y2, y1 - l1, y1 + l1, x1); return !(check1 && check2 && check3 && check4); } static boolean checkXLine(int min1, int max1, int min2, int max2, int y1, int y2) { if (y1 != y2) return true; if (min1 > max2 || max1 < min2) return true; return false; } static boolean checkYLine(int min1, int max1, int min2, int max2, int x1, int x2) { if (x1 != x2) return true; if (min1 > max2 || max1 < min2) return true; return false; } static boolean checkXYLine(int minX, int maxX, int Y, int minY, int maxY, int X) { if (X < minX || X > maxX) return true; if (Y < minY || Y > maxY) return true; return false; } }