We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
New learning.. Thank you for the question.
Converted the editorial version to Java
publicclassSolution{privatestaticfinalScannerscanner=newScanner(System.in);publicstaticvoidmain(String[]args){String[]wh=scanner.nextLine().split(" ");intw=Integer.parseInt(wh[0]);inth=Integer.parseInt(wh[1]);String[]circleXCircleY=scanner.nextLine().split(" ");intcircleX=Integer.parseInt(circleXCircleY[1]);intcircleY=Integer.parseInt(circleXCircleY[0]);intr=Integer.parseInt(circleXCircleY[2]);Circlecircle=newCircle(newPoint(circleX,circleY),r);String[]x1Y1X3Y3=scanner.nextLine().split(" ");intx1=Integer.parseInt(x1Y1X3Y3[1]);inty1=Integer.parseInt(x1Y1X3Y3[0]);intx3=Integer.parseInt(x1Y1X3Y3[3]);inty3=Integer.parseInt(x1Y1X3Y3[2]);Squaresquare=Square.read(x1,y1,x3,y3);// Write Your Code Herefor(inti=0;i<h;i++){for(intj=0;j<w;j++){if(circle.contains(i,j)||square.contains(i,j)){System.out.print("#");}else{System.out.print(".");}}System.out.println();}scanner.close();}staticclassPoint{doublex;doubley;Point(doublei,doublej){this.x=i;this.y=j;}}staticclassCircle{Pointcenter;intr;publicCircle(Pointpt,intr){this.center=pt;this.r=r;}booleancontains(inti,intj){doubledi=i-center.x;doubledj=j-center.y;return(di*di)+(dj*dj)<=r*r;}}staticclassSquare{Pointp1,p2,p3,p4;publicSquare(Pointp1,Pointp2,Pointp3,Pointp4){this.p1=p1;this.p2=p2;this.p3=p3;this.p4=p4;}booleancontains(inti,intj){returnnewTriangle(p1,p3,p2).contains(newPoint(i,j))||newTriangle(p1,p3,p4).contains(newPoint(i,j));}staticSquareread(intx1,inty1,intx3,inty3){//center pointsdoublecx=(x1+x3)/2.0;doublecy=(y1+y3)/2.0;//vector from center to x1, y1doubledx=x1-cx;doubledy=y1-cy;//vecors perpendiculardoublepx=-1*dy;doublepy=dx;doublex2=cx+px;doubley2=cy+py;doublex4=cx-px;doubley4=cy-py;returnnewSquare(newPoint(x1,y1),newPoint(x2,y2),newPoint(x3,y3),newPoint(x4,y4));}}staticclassTriangle{Pointp1,p2,p3;doublearea;publicTriangle(Pointp1,Pointp2,Pointp3){this.p1=p1;this.p2=p2;this.p3=p3;this.area=Math.abs(((p3.x-p1.x)*(p2.y-p1.y))-((p3.y-p1.y)*(p2.x-p1.x)))/2;}booleancontains(Pointp){returnthis.area==newTriangle(p1,p2,p).area+newTriangle(p2,p3,p).area+newTriangle(p3,p1,p).area;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Circle and a Square
You are viewing a single comment's thread. Return to all comments →
New learning.. Thank you for the question. Converted the editorial version to Java